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

sirab333
  • 3,662
  • 8
  • 41
  • 54
  • set the 2nd Button after you push the ViewController or set the 2nd Button in your specific itemsByTitleVC Controller – CarlJ Apr 18 '12 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... – sirab333 Apr 18 '12 at 12:13

4 Answers4

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;
Prine
  • 12,192
  • 8
  • 40
  • 59
  • 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... – sirab333 Apr 18 '12 at 12:12
  • 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.. – Prine Apr 18 '12 at 12:15
  • 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; ` – sirab333 Apr 18 '12 at 12:21
  • 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. – Prine Apr 18 '12 at 12:24
  • 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... – sirab333 Apr 18 '12 at 12:55
  • Its _almost_ working: the button shows up for a second- then immediately fades away and disappears. Very weird. But I think I know why: I have a .png image in my NavControllerBar - its like a header/logo graphic - which I've added as a subview. Maybe its "covering up" the Search button. Except you'd think it would also cover up the "back" button on the Left side of the NavBar. Who knows. Let me test this... – sirab333 Apr 18 '12 at 13:00
  • Yeah that was it: once I commented out the header-graphic code, I could see the search-button sitting there, all pretty :-) Guess the left Back button is "protected" by the iOS from being hidden while the right buttons are not... So now I'll have to figure out a hack to get both searchButton and Header graphic to show at the same time... – sirab333 Apr 18 '12 at 13:08
  • Hmm strange I also use a header graphic for my navigation bar. But I can see buttons on the right. How do you set the navigationbar background? I do it like that: // Define the NavigationBar header image // Do any additional setup after loading the view from its nib. if ([self.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){ [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar.png"] forBarMetrics:UIBarMetricsDefault]; } – Prine Apr 18 '12 at 13:10
  • If your way works then I should definitely try your way :-) Do you put that in the viewDidLoad of the controller you're navigating to? or in the view-vontroller you're navigating _from_? Here's the way I've been doing it: `// Banner Code: UIImageView *bannerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; [bannerImageView setImage:[UIImage imageNamed:@"header.gif"]]; [self.navigationController.navigationBar addSubview:bannerImageView]; ` – sirab333 Apr 18 '12 at 16:35
  • Works! :-) Doing it your way allows for both a header graphic and the right-bar button. Awesome :-) Learned something new today - many thanks! Will mark your answer as the correct one. – sirab333 Apr 18 '12 at 16:42
0

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

clowwindy
  • 1,020
  • 8
  • 12
  • again, good catch - but this was merely a typo in this posting - which I've corrected (see above) - I'm consistently using "searchButton" in the code, so that isn't the problem... – sirab333 Apr 18 '12 at 12:16
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.

ЯegDwight
  • 24,821
  • 10
  • 45
  • 52
Gon
  • 1,135
  • 12
  • 22
-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];.