Question

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...)

Was it helpful?

Solution

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;

OTHER TIPS

Maybe you mixed searchButton up with filterButton?

EDIT:

Once again I looked at your code and I noticed that you are doing this in :didSelectRowAtIndexPath.

When you set self.navigationItem.rightBarButtonItem, actually your are changing the button of current navigationController, not the button of itemsByTitleVC.

So the resolution is, move this piece of code into somethere inside itemsByTitleVC, like viewDidLoad: or viewWillAppear:.

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html

I ran into this problem today, and eventually I found out that my (and your)

self.navigationItem.rightBarButtonItem 

is a readonly property of the UIView class.

Declare your own property that points to the barbuttonItem and you can modify the button.

I have happened with this problem, and finally I solved it by using setter method to set rightBarButtonItem. Like [self.navigationItem setRightBarButtonItem:saveButton animated:YES];.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top