UIBarButtonItem not showing up in UINavigationController

sirab333 picture sirab333 · Apr 18, 2012 · Viewed 7.7k times · Source

I'm trying to add 2 buttons to a UINavigationController's navigation-bar:

1) the standard "back" button on the left side - which works, and

2) a "search" button on the right side - which does not show up.

Here's the code:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

// 1st button - this shows up correctly:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];
backButton.title = @"MAIN";
self.navigationItem.backBarButtonItem = backButton;    

// 2nd. button - this one does not show up:
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc]
                                           initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
                                           target:self
                                           action:@selector(goSearching:)];
self.navigationItem.rightBarButtonItem = searchButton;


itemsByTitleVC *itemsView = [[itemsByTitleVC alloc] initWithNibName:@"itemsByTitleVC" bundle:nil];


[self.navigationController pushViewController:itemsView animated:YES];

}

anyone see why this isn't working? (For what its worth, I'm using Xcode 4.2, with Storyboard...)

Answer

Prine picture Prine · Apr 18, 2012

Your setting the rightBarButtonItem to filterButton, but shouldn't it be searchButton?

// 2nd. button - this one does not show up:
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc]
                                           initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
                                           target:self
                                           action:@selector(goSearching:)];
// Here I think you wanna add the searchButton and not the filterButton..
self.navigationItem.rightBarButtonItem = searchButton;