I want to load another view when I click on one of my UITableCell view, but none of the things that are on the xib file (associated with that view) are showing.
This is how I'm initializing the view (in the controller that generates the tableView):
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Cards";
self.detailController = [[BasicCardViewController alloc] initWithNibName: #"CardView" bundle:nil];
}
return self;
}
This is where I deal with the selection:
- (void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *component = self.resultsTuples[indexPath.row];
[AJAXUtils getAsyncJsonWithUrl:(NSURL *)[NSURL URLWithString:someUrl] callback:^(NSDictionary *returnjson) {
if (returnjson != nil) {
NSString *userPageLink = returnjson[#"Node"][#"SessionInfo"][#"PostingAs"][#"Key"];
self.detailController.userPageLink = userPageLink;
self.detailController.nodePage = returnjson[#"Node"][#"Key"];
NSString *selectedCard = component[#"$element"][#"Title"];
[self.detailController setDescription:component[#"element"][#"ContactCard"][#"Description"]];
[self.detailController setPageTitle:selectedCard];
self.detailController.title = selectedCard;
NSString* rating = component[#"$element"][#"Summary"][#"AverageRating"];
self.detailController.rating =(NSInteger)rating;
[self.navigationController pushViewController:self.detailController animated:YES];
}
}];
}
This is my BasicCardView code -
#implementation BasicCardViewController
#synthesize userPageLink = _userPageLink;
#synthesize nodePage = _nodePage;
- (void)viewDidLoad {
_trendingImageView.image = [UIImage imageNamed:#"trending.png"];
}
- (UILabel *)label {
return (id)self.view;
}
- (void)loadView {
self.rateView = [[RateView alloc] init];
}
- (void)setDescription:(NSString *)description {
_description = description;
_descriptionView.text = description;
}
- (void)setPageScore:(NSString *)pageScore {
_pageScore = pageScore;
_pageScoreLabel.text = pageScore;
}
- (void)setRestaurantImage:(UIImage *)restaurantImage {
_restaurantImage.image = restaurantImage;
}
- (void)setPageTitle:(NSString *)title {
_pageTitle = title;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear: animated];
self.rateView.notSelectedStar =[UIImage imageNamed:#"kermit_empty.png"];
self.rateView.halfSelectedStar = [UIImage imageNamed:#"kermi_half.png"];
self.rateView.fullSelectedStar = [UIImage imageNamed:#"kermit_full.png"];
self.rateView.rating = self.rating;
self.rateView.editable = YES;
self.rateView.maxRating = 5;
self.rateView.delegate = self;
_pageTitleLabel.text = _pageTitle;
}
Why is nothing appearing when the cell is clicked?
But if I do self.view = rateView in the viewDidLoad, the rateView appears.
The view controller calls loadView method when its view property is requested but is currently nil. This method loads or creates a view and assigns it to the view property. And it appeared as if your self.view is nil, in loadView method, you have created self.rateView but hasn't assigned it to self.view to make it visible.
Maybe the getAsyncJsonWithUrl block called out of the main thread try, to call the pushViewController inside the mainQueue as following:
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self.navigationController pushViewController:self.detailController animated:YES];
}];
It may help sometimes.
_detailController = [[BasicCardViewController alloc] initWithNibName: #"CardView" bundle:nil];
Related
i want to hide the side menu whenever the user taps anywhere outside. The menu is appearing but i can't find any way to come out of it. any suggestions regarding hidesOnTap will also be appreciated. I have also used a table view to display side menu contents, whenever user taps on any option it gets redirected to that page. How to hide the menu without making any selection. Below is the code
#import "MainMenuViewController.h"
#import "UIImageView+AFNetworking.h"
#import "ProfileViewController.h"
#import "ChatMenuHomeVC.h"
#interface MainMenuViewController ()
{
NSString *username;
NSString *profilestr;
}
#property (nonatomic, weak) IBOutlet UITableViewCell *cellMain;
#end
#implementation MainMenuViewController
#synthesize lblUserName,profilePic;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
//SideMenuToProfileView
arrOfOptions = [[NSMutableArray alloc]init];
[self addTitle:#"Home" imageName:#"icon-sidemenu-home.png" segueName:#"slidingToHome"];
[self addTitle:#"My Chat" imageName:#"icon-sidemenu-chat.png" segueName:#"ChatHomeVc"];
[self addTitle:#"Invite Friends" imageName:#"icon-sidemenu-invite.png" segueName:#""];
[self addTitle:#"Settings" imageName:#"icon-sidemenu-setting.png" segueName:#"slidingToSettings"];
[super viewDidLoad];
UITapGestureRecognizer *tapAction1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(profileVC:)];
tapAction1.numberOfTapsRequired = 1;
profilePic.userInteractionEnabled = YES;
[profilePic addGestureRecognizer:tapAction1];
username = [[NSUserDefaults standardUserDefaults]
stringForKey:#"UserName"];
profilestr = [[NSUserDefaults standardUserDefaults] stringForKey: #"UserProfilePic"];
self.lblUserName.text = username;
[profilePic setImageWithURL:[NSURL URLWithString:profilestr] placeholderImage:[UIImage imageNamed:#"user"]];
self.profilePic.layer.cornerRadius = self.profilePic.frame.size.width / 2;
self.profilePic.clipsToBounds = YES;
// Do any additional setup after loading the view.
}
-(void)addTitle:(NSString *)title imageName:(NSString *)imageName segueName:(NSString *)segue
{
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
[dic setObject:title forKey:#"title"];
[dic setObject:imageName forKey:#"image"];
[dic setObject:segue forKey:#"segue"];
[arrOfOptions addObject:dic];
}
- (void)profileVC:(UITapGestureRecognizer *)tapGesture
{
[self performSegueWithIdentifier:#"SideMenuToProfileView" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"SideMenuToProfileView"])
{
ProfileViewController *destViewController = segue.destinationViewController;
destViewController.senderStr = #"fromUserSideMenu";
}
}
create a background View with size equal to the device in width and height. Add tap gesture recognizer to the view and hide side menu when the view is tapped. Or you can also use a button to get the same functionality writing the code in button action method.
Do as follows
var backgroundView : UIView = UIView()
func createBackgroundView()
{
backgroundView = UIView(frame: CGRect(x: 0, y: 0, width: ScreenSize.SCREEN_WIDTH, height: ScreenSize.SCREEN_HEIGHT))
backgroundView.tag = 10
backgroundView.isHidden = true
}
You can unhide the background view when side menu appears
In my app I have a UITabBarController which has four view controllers. In every view controller I have a collection view. In each collection view I have some images. When I select an image from the third view controller it opens a web view controller which is not in UITabBarController.
In web view controller I have a back button on top with navigation bar. After pressing that back button it's coming back to third view controller. Again when I select another image in third view controller, it should open the web view controller, but the web view controller didn't appear on simulator instead saying error 1thread,1breakpoint.
Here is my code:
This code is in thirdviewcontroller after selecting the image
webViewController = [[AppsWebViewController alloc]init];
[self presentViewController:webViewController animated:YES completion:nil];
This code is in webviewcontroller after pressing the back button
[self dismissViewControllerAnimated:YES completion:nil];
I need the webviewcontroller to open every time after selecting any image in any of the four view controllers.
my thirdviewcontroller code:
#import "FreqAppsThirdViewController.h"
#interface FreqAppsThirdViewController ()
#end
#implementation FreqAppsThirdViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
icons = [NSArray arrayWithObjects:#"amazon-1.png",#"best_buy.png",#"Carl-Icahn-Lectures-Apple-Gambles-Netflix-and-Threatens-eBay-2.jpg",#"index.jpg",#"Office-Max.jpg", nil];
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
_collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
[_collectionView setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:_collectionView];
[super viewDidLoad];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return icons.count;
}
// The cell that is returned must be retrieved from a call to - dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:[icons objectAtIndex:indexPath.row]]];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionViewdidSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
NSURL *url = [NSURL URLWithString:#"http://www.amazon.com/"];
webViewController = [[FreqAppsWebViewController alloc]initWithURL:url andTitle:#"Amazon"];
[self presentViewController:webViewController animated:YES completion:nil];
} else if (indexPath.row == 1){
NSURL *url = [NSURL URLWithString:#"http://www.bestbuy.com/"];
webViewController = [[FreqAppsWebViewController alloc]initWithURL:url andTitle:#"Best Buy"];
[self presentViewController:webViewController animated:YES completion:nil];
} else if (indexPath.row == 2){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.ebay.com"]];
} else if (indexPath.row == 3){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.walmart.com"]];
} else if (indexPath.row == 4){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.officemax.com"]];
}
// datasetCell.backgroundColor = [UIColor blueColor]; // highlight selection
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(50, 50);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout(UICollectionViewLayout*)collectionViewLayoutinsetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(50, 20, 50, 20);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
my webviewcontroller code:
#import "FreqAppsWebViewController.h"
#import "FreqAppsThirdViewController.h"
#import "FreqAppsAppDelegate.h"
#interface FreqAppsWebViewController ()
#end
#implementation FreqAppsWebViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (id)initWithURL:(NSURL *)url andTitle:(NSString *)string {
if( self = [super init] ) {
theURL = url;
theTitle = string;
}
return self;
}
-(id)initWithURL:(NSURL *)url {
return [self initWithURL:url andTitle:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
webTitle.title = theTitle;
NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];
[webView loadRequest:requestObject];
}
- (IBAction) back:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
webView.delegate = nil;
[webView stopLoading];
}
#end
my webviewcontroller.h code
#import <UIKit/UIKit.h>
#interface FreqAppsWebViewController : UIViewController <UIWebViewDelegate>
{
NSURL *theURL;
NSString *theTitle;
IBOutlet UIWebView *webView;
IBOutlet UINavigationItem *webTitle;
}
- (id)initWithURL:(NSURL *)url;
- (id)initWithURL:(NSURL *)url andTitle:(NSString *)string;
- (IBAction)back:(id)sender;
#end
my thirdviewcontroller.h code:
#import <UIKit/UIKit.h>
#import "FreqAppsWebViewController.h"
#interface FreqAppsThirdViewController:UIViewController<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
UICollectionView *_collectionView;
NSArray *icons;
FreqAppsWebViewController *webViewController;
}
#end
Copy and paste the full error message you're getting.
If it is a breakpoint, not an error, as you seem to be indicating, it might just be that you clicked on a the margin of your source and set a breakpoint without realizing it.
To check for breakpoints:
Press Command 7 to display the breakpoint navigator and see if there are any breakpoints set in your code. If there are, you'll an outline starting with the target (the current app) then the source file .m, and then an entry listing a method name and line number, with a symbol that looks like a cross between a right-pointing arrow and a blue sticky note.
If there are breakpoints, select them one at a time and note where they are in your code. Are then in your IBAction method, or the one of the methods in your AppsWebViewController?
Delete each breakpoint by selecting it and pressing the delete key. Then run your program again.
I am new to iOS development, and I am having some issues showing images in a simple page view controller photo gallery.
The problem I am having is that the images in the child view won't be displayed. The only thing showing is a blank page.
Here is the view controller file holding page view controller:
#import "RTPGalleryViewController.h"
#import "RTPSinglePhotoViewController.h"
#import "JMImageCache.h"
#interface RTPGalleryViewController ()
#end
#implementation RTPGalleryViewController
#synthesize photosArray, pageViewController, pagesArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
pagesArray = [[NSMutableArray alloc] init];
[self generatePhotoViews];
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageViewController.dataSource = self;
[[self.pageViewController view] setFrame:[[self view] bounds]];
RTPSinglePhotoViewController *initialViewController = [pagesArray objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30);
[self addChildViewController:self.pageViewController];
[[self view] addSubview:[self.pageViewController view]];
[self.pageViewController didMoveToParentViewController:self];
// Do any additional setup after loading the view.
}
-(void) initWithPhotoArray:(NSMutableArray *) photos{
self.photosArray = photos;
}
-(void) generatePhotoViews{
for (int i=0; i< [photosArray count]; i++) {
RTPSinglePhotoViewController *view = [[RTPSinglePhotoViewController alloc] init] ;
view.imageUrl = [photosArray objectAtIndex:i];
view.pageIndex = i;
[pagesArray addObject:view];
}
}
- (RTPSinglePhotoViewController *)viewControllerAtIndex:(NSUInteger)index
{
NSLog(#"PHOTO ARRAY COUNT IS: %lu", (unsigned long)[photosArray count]);
if ([self.pagesArray count] == 0) {
return nil;
}
return [pagesArray objectAtIndex:index];
;
}
#pragma mark - Page View Controller Data Source
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = ((RTPSinglePhotoViewController*) viewController).pageIndex;
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
NSLog(#"THE INDEX NOW IS %lu",(unsigned long)index);
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((RTPSinglePhotoViewController*) viewController).pageIndex;
if (index == ([pagesArray count]-1) || index == NSNotFound) {
return nil;
}
NSLog(#"THE INDEX NOW IS %lu",(unsigned long)index);
index++;
return [self viewControllerAtIndex:index];
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return [self.photosArray count];
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return 0;
}
#end
RTPSinglePhotoViewController is the content view controller holding images here is the code:
#import "RTPSinglePhotoViewController.h"
#import "JMImageCache.h"
#interface RTPSinglePhotoViewController ()
#end
#implementation RTPSinglePhotoViewController
#synthesize imageView,imageUrl;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString * path = imageUrl;
NSURL * url = [NSURL URLWithString:path];
// [cell.thumbnailImageView setImageWithURL:url key:nil placeholder:nil completionBlock:nil failureBlock:nil];
[[JMImageCache sharedCache] imageForURL:url completionBlock:^(UIImage *downloadedImage) {
imageView.image = downloadedImage;
} failureBlock:nil];
// Do any additional setup after loading the view.
}
#end
Now, according to the log, the image is being downloaded/cached properly, it is just not showing up. Seeing a blank page instead of photo. What am I doing wrong?
Any suggestions would be appreciated!
I suppose you are not using storyboard.
The way you are allocating view controllers is:
RTPSinglePhotoViewController *view = [[RTPSinglePhotoViewController alloc] init] ;
Where it should be:
RTPSinglePhotoViewController *view = [[RTPSinglePhotoViewController alloc] initWithNibName:#"The name of the xib associated with this controller" bundle: [NSBundle mainBundle]];
The problem is that you are not associating any view to your view controller, therefore nothing is displayed
EDIT: Since you're using storyboards the right way to instantiate the view controller is:
RTPSinglePhotoViewController *view = (RTPSinglePhotoViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"your_controllers_storyboard_id"];
I have a root view controller which composes a search bar on the top and a child table view controller on the bottom. I used composition instead of assigning the search bar to the table view's header for these reasons:
I didn't want the index to overlap with the search bar (like Contacts app).
I wanted the search bar to be sticky. That is, it doesn't move when I scroll the table view (again like the Contacts app).
My table view already had a header.
Since the search bar is in the root view controller, I instantiate my search display controller in the root view controller also. There are two problems with the search UI for which I seek advice:
The translucent gray overlay does not cover the entire child table view. It leaves the top portion of the header and the index visible.
Likewise, the search results table does not cover the entirety of the child table view. I know how to manually change the frame of this results table view, but doing so only fixes just that ... the gray translucent overlay's frame is not linked to the results table view frame. Their is no property to access the overlay.
1) Idle
2) Enter Search Bar
3) Start Typing
#import "ContactsRootViewController.h"
#import "ContactsViewController.h"
#import "UIView+position.h"
#import "User.h"
#import "UserCellView.h"
#import "UserViewController.h"
#interface ContactsRootViewController ()
#property(nonatomic, strong) UISearchBar* searchBar;
#property(nonatomic, strong) ContactsViewController* contactsViewController;
#property(nonatomic, strong) UISearchDisplayController* searchController;
#property(nonatomic, strong) NSMutableArray* matchedUsers;
#end
#implementation ContactsRootViewController
#pragma mark UIViewController
- (NSString*)title
{
return #"Contacts";
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.matchedUsers = [NSMutableArray array];
self.searchBar = [[UISearchBar alloc] init];
self.searchBar.placeholder = #"Search";
[self.searchBar sizeToFit];
[self.view addSubview:self.searchBar];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (self.contactsViewController == nil) {
self.contactsViewController = [[ContactsViewController alloc] init];
[self addChildViewController:self.contactsViewController];
self.contactsViewController.view.frame = CGRectMake(
0.0,
self.searchBar.bottomY,
self.view.frame.size.width,
self.view.frame.size.height - self.searchBar.bottomY
);
self.contactsViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:self.contactsViewController.view];
[self.contactsViewController didMoveToParentViewController:self];
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self.contactsViewController];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
}
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.matchedUsers.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* identifier = #"contactsRootViewUserCell";
UserCellView* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UserCellView alloc] initWithIdentifier:identifier];
}
cell.user = [self.matchedUsers objectAtIndex:indexPath.row];
return cell;
}
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.navigationController pushViewController:[[UserViewController alloc] initWithUser:[self.matchedUsers objectAtIndex:indexPath.row]] animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [UserCellView height];
}
#pragma mark UISearchDisplayControllerDelegate
- (BOOL)searchDisplayController:(UISearchDisplayController*)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self.matchedUsers removeAllObjects];
searchString = [searchString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (searchString.length > 0) {
for (User* user in self.contactsViewController.allUsers) {
NSRange match = [user.userDisplayName rangeOfString:searchString options:NSCaseInsensitiveSearch];
if (match.location != NSNotFound) {
[self.matchedUsers addObject:user];
}
}
}
return YES;
}
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
[self.searchBar resignFirstResponder];
}
#end
I re-implemented UISearchDisplayController, calling my implementation SearchController. It does the same thing and has similar delegate callbacks, but the frame of the search results can be controlled by the programmer.
Header
#import <Foundation/Foundation.h>
#class SearchController;
#protocol SearchControllerDelegate <NSObject>
#required
- (BOOL)searchController:(SearchController*)controller shouldReloadTableForSearchString:(NSString*)searchText;
#optional
- (void)searchController:(SearchController*)controller didShowSearchResultsTableView:(UITableView*)tableView;
- (void)searchController:(SearchController *)controller didHideSearchResultsTableView:(UITableView *)tableView;
- (void)searchControllerDidBeginSearch:(SearchController*)controller;
- (void)searchControllerDidEndSearch:(SearchController*)controller;
#end
#interface SearchController : UIViewController <UISearchBarDelegate>
#property(nonatomic, weak) NSObject<SearchControllerDelegate>* delegate;
#property(nonatomic, weak) NSObject<UITableViewDataSource>* searchResultsDataSource;
#property(nonatomic, weak) NSObject<UITableViewDelegate>* searchResultsDelegate;
#property(nonatomic, strong, readonly) UITableView* searchResultsTableView;
- (id)initWithSearchBar:(UISearchBar*)searchBar;
#end
Implementation
#import "SearchController.h"
#import "UIView+position.h"
#interface SearchController ()
#property(nonatomic, strong) UISearchBar* searchBar;
#property(nonatomic, strong) UIButton* searchResultsVeil;
#property(nonatomic, strong, readwrite) UITableView* searchResultsTableView;
#property(nonatomic, assign) BOOL searchResultsTableViewHidden;
- (void)didTapSearchResultsVeil;
- (void)hideSearchResults;
#end
#implementation SearchController
#pragma mark UIViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.searchResultsTableView deselectRowAtIndexPath:[self.searchResultsTableView indexPathForSelectedRow] animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.userInteractionEnabled = NO;
}
#pragma mark SearchController ()
- (void)hideSearchResults
{
self.searchBar.text = nil;
[self.searchResultsTableView reloadData];
self.searchResultsTableViewHidden = YES;
[self.searchBar resignFirstResponder];
}
- (void)didTapSearchResultsVeil
{
[self hideSearchResults];
}
- (void)setSearchResultsTableViewHidden:(BOOL)searchResultsTableViewHidden
{
if (self.searchResultsTableView != nil) {
if (self.searchResultsTableView.hidden && !searchResultsTableViewHidden) {
self.searchResultsTableView.hidden = searchResultsTableViewHidden;
if ([self.delegate respondsToSelector:#selector(searchController:didShowSearchResultsTableView:)]) {
[self.delegate searchController:self didShowSearchResultsTableView:self.searchResultsTableView];
}
} else if (!self.searchResultsTableView.hidden && searchResultsTableViewHidden) {
self.searchResultsTableView.hidden = searchResultsTableViewHidden;
if ([self.delegate respondsToSelector:#selector(searchController:didHideSearchResultsTableView:)]) {
[self.delegate searchController:self didHideSearchResultsTableView:self.searchResultsTableView];
}
}
}
}
- (BOOL)searchResultsTableViewHidden
{
return self.searchResultsTableView == nil || self.searchResultsTableView.hidden;
}
#pragma mark SearchController
- (id)initWithSearchBar:(UISearchBar *)searchBar
{
if (self = [super init]) {
self.searchBar = searchBar;
self.searchBar.delegate = self;
}
return self;
}
- (void)setSearchResultsDataSource:(NSObject<UITableViewDataSource> *)searchResultsDataSource
{
_searchResultsDataSource = searchResultsDataSource;
if (self.searchResultsTableView != nil) {
self.searchResultsTableView.dataSource = searchResultsDataSource;
}
}
- (void)setSearchResultsDelegate:(NSObject<UITableViewDelegate> *)searchResultsDelegate
{
_searchResultsDelegate = searchResultsDelegate;
if (self.searchResultsTableView != nil) {
self.searchResultsTableView.delegate = searchResultsDelegate;
}
}
#pragma mark UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if ([self.delegate searchController:self shouldReloadTableForSearchString:searchText]) {
[self.searchResultsTableView reloadData];
self.searchResultsTableViewHidden = [self.searchResultsTableView.dataSource tableView:self.searchResultsTableView numberOfRowsInSection:0] == 0;
}
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES animated:YES];
if (self.searchResultsVeil == nil) {
self.searchResultsVeil = [[UIButton alloc] initWithFrame:self.view.bounds];
self.searchResultsVeil.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.6];
self.searchResultsVeil.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.searchResultsVeil addTarget:self action:#selector(didTapSearchResultsVeil) forControlEvents:UIControlEventTouchUpInside];
self.searchResultsTableView = [[UITableView alloc] initWithFrame:self.searchResultsVeil.bounds style:UITableViewStylePlain];
self.searchResultsTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
if ([self.searchResultsTableView respondsToSelector:#selector(setSeparatorInset:)]) {
self.searchResultsTableView.separatorInset = UIEdgeInsetsMake(
0.0,
self.searchResultsTableView.width,
0.0,
0.0
);
}
self.searchResultsTableViewHidden = YES;
if (self.searchResultsDataSource != nil) {
self.searchResultsTableView.dataSource = self.searchResultsDataSource;
}
if (self.searchResultsDelegate != nil) {
self.searchResultsTableView.delegate = self.searchResultsDelegate;
}
[self.view addSubview:self.searchResultsVeil];
[self.searchResultsVeil addSubview:self.searchResultsTableView];
}
self.view.userInteractionEnabled = YES;
self.searchResultsVeil.hidden = NO;
if ([self.delegate respondsToSelector:#selector(searchControllerDidBeginSearch:)]) {
[self.delegate searchControllerDidBeginSearch:self];
}
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:NO animated:YES];
self.view.userInteractionEnabled = NO;
self.searchResultsVeil.hidden = YES;
if ([self.delegate respondsToSelector:#selector(searchControllerDidEndSearch:)]) {
[self.delegate searchControllerDidEndSearch:self];
}
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[self hideSearchResults];
}
#end
Usage
self.searchController = [[SearchController alloc] initWithSearchBar:self.searchBar];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
[self addChildViewController:self.searchController];
self.searchController.view.frame = CGRectMake(
self.searchBar.x,
self.searchBar.bottomY,
self.searchBar.width,
self.view.height - self.searchBar.bottomY
);
[self.view addSubview:self.searchController.view];
[self.searchController didMoveToParentViewController:self];
It looks like your view controller does not define a presentation context. I had a similar problem and was able to resolve it by setting
self.definesPresentationContext = YES;
in viewDidLoad. According to the documentation this property is
A Boolean value that indicates whether this view controller's view is covered when the view controller or one of its descendants presents a view controller.
I know there are already lots of questions about this but none of them have helped me yet. I have two UITableViewControllers. I want to push a QuicklistViewController (using this like a quick menu to list different shows) onto a CurrentShowViewController.
I have a Show model that only contains a name property right now. I am have trouble moving the data from my selection in QuicklistViewController to the CurrentShowViewController so that I can display the currentShow information there.
I am totally stuck on how I can do this.Please help!
Here is my CurrentShowViewController:
#import "CurrentShowViewController.h"
#import "QuicklistViewController.h"
#interface CurrentShowViewController ()
#end
#implementation CurrentShowViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
self.tabBarItem.image = [UIImage imageNamed:#"tab_icon_episodes.png"];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
//self.navigationItem.rightBarButtonItem = self.editButtonItem;
// Add a UIBarButton button that will display a Quicklist Modal View
UIBarButtonItem *quicklistButton = [[UIBarButtonItem alloc] initWithTitle:#"Quicklist"
style:UIBarButtonItemStylePlain
target:self
action:#selector(quicklistButtonPressed)];
self.navigationItem.leftBarButtonItem = quicklistButton;
self.currentShow = [[Show alloc] init];
NSLog(#"Current show name is: %#", self.currentShow.name);
self.title = self.currentShow.name;
}
- (void) viewWillAppear:(BOOL)animated {
self.title = self.currentShow.name;
}
- (void) quicklistButtonPressed {
QuicklistViewController *quicklistVC = [[QuicklistViewController alloc] init];
[self.navigationController pushViewController:quicklistVC animated:YES];
}
Here is my QuicklistViewController:
#import "QuicklistViewController.h"
#import "CurrentShowViewController.h"
#interface QuicklistViewController ()
#end
#implementation QuicklistViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
self.title = #"Quicklist";
self.tabBarItem.image = [UIImage imageNamed:#"tab_icon_quicklist.png"];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:self
action:#selector(backButtonPressed)];
self.navigationItem.leftBarButtonItem = backButton;
// Create a temp array of Show objects filled with test data
NSMutableArray *temp= [[NSMutableArray alloc] init];
[temp addObject:[[Show alloc] initWithName:#"The Walking Dead"]];
[temp addObject:[[Show alloc] initWithName:#"How I Met Your Mother"]];
[temp addObject:[[Show alloc] initWithName:#"Grey's Anatomy"]];
[temp addObject:[[Show alloc] initWithName:#"The Mentalist"]];
[temp addObject:[[Show alloc] initWithName:#"Stargate SG1"]];
NSArray *testShows = [[NSArray alloc] initWithArray:temp];
self.shows = testShows;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.currentShow = [[Show alloc] init];
}
- (void) backButtonPressed {
//Archive changes to currentShow object
[Show saveShow:self.currentShow];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.shows count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
}
cell.textLabel.text = [self.shows[indexPath.row] name];
return cell;
}
- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Change the current show to the selected show
self.currentShow = self.shows[indexPath.row];
//Archive changes to currentShow object
[Show saveShow:self.currentShow];
}
Use delegation.
CurrentShowViewController
- (void) quicklistButtonPressed
{
QuicklistViewController *quicklistVC = [[QuicklistViewController alloc] init];
quicklistVC.delegate = self;
[self.navigationController pushViewController:quicklistVC animated:YES];
}
- (void) selectedShow:(Show) show
{
// show : current show
}
QuicklistViewController
#protocol QuicklistViewControllerDelegate
- (void) selectedShow:(Show) show
#end
#property (nonatomic, weak) id delegate;
- (void) backButtonPressed
{
//Archive changes to currentShow object
[Show saveShow:self.currentShow];
[delegate selectedShow:self.currentShow];
[self.navigationController popViewControllerAnimated:YES];
}
You've mistakenly implemented didDeselectRowAtIndexPath instead of didSelectRowAtIndexPath.