0

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

2
  • set the 2nd Button after you push the ViewController or set the 2nd Button in your specific itemsByTitleVC Controller Commented Apr 18, 2012 at 12:00
  • neither worked. I tried before to add the rightBarButtonItem in the viewDidLoad of the viewController that is being pushed-on -- didn't work... Commented Apr 18, 2012 at 12:13

3 Answers 3

5

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;
Sign up to request clarification or add additional context in comments.

10 Comments

sorry :-) that was indeed a typo on my part - but only in this post - I have it typed right in the code - and it doesn't work...
But have you tried to create the right bar button in the itemsByTitleVC Controller in the viewDidLoad method? Looks like your setting the rightBarButton but it does get overridden in the new view. The left button is maybe working because its the standard back button which is always displayed..
I did try setting the search button in the viewDidLoad method of "itemsByTitleVC" viewController - it still didn't work. Here's the code I used - its the exact same code - but maybe it needs tweaking since its in the viewDidLoad now?: UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(goSearching:)]; self.navigationItem.rightBarButtonItem = searchButton;
I just had a look in my own project, where I'm doing the exact same thing. Here is the code I'm using. // Create left Scan button UIBarButtonItem *scanButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(scanButtonClicked:)]; [self.navigationItem setLeftBarButtonItem:scanButton]; and I do it also in the viewDidLoad method from the new pushed ViewController.
I see you are using [self.navigationItem setLeftBarButtonItem:scanButton]; - in other words, you're using a method, while I've been using dot-syntax self.navigationItem.rightBarButtonItem = searchButton; I wonder if that's the issue...lets see...
|
0

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.

Comments

-1

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];.

1 Comment

using the setter makes not difference here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.