Nirav Bhatt
Nirav Bhatt

Reputation: 6969

iOS Right navigation button does not display in UINavigationBar

I have following view controllers hierarchy:

In VC1, I do this:

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

This rightly brings up tab bar controller, with TVC1 in focus.

TVC1 shows back button in its navigation bar (programmatically created from VC1 code), which will get me to VC1, which is expected.

However, from TVC1 onwards, I need one more navigation to TVC2. I am trying to add right button to the TVC1 navigation bar for this, but it doesn't show up.

Here is the code I use in TVC1 (rightButton is UIButton type property of TVC1):

   self.rightButton = [[UIBarButtonItem alloc]
                            initWithBarButtonSystemItem: UIBarButtonSystemItemAdd
                            target: self
                            action: @selector(MySelector:)];
   self.rightButton.style = UIBarButtonItemStyleBordered;
   self.rightButton.title = @"";
   self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:   self.rightButton, nil];

(specified blank title and style just to ensure if that's the issue which is causing this, I don't actually need those values)

MySelector is declared in TVC1.h as:

- (void) MySelector:(id)sender;

And it is properly implemented, too.

But rightButton above does not display in TVC1 navigation bar.

What am I missing? I suspect its with TBC1 (tab bar) that comes between VC1 and TVC1, and somehow it resets navigation properties.

But then I argue that I see navigation bar on TVC1, and a left button leading to VC1. I checked that in TBC1, self.navigationItem.rightBarButtonItems has 1 object inside which is definitely the rightButton I am adding.

Where am I wrong?

Note: Above is found in all of iOS 5.0, 5.1 and 6.0 simulators.

Upvotes: 4

Views: 4037

Answers (2)

AMAN77
AMAN77

Reputation: 6302

Had the same problem recently in Swift and found that embedding the child view in a navigationController was still the correct way to be able to access the rightBarButtonItems.

Upvotes: 0

Paresh Masani
Paresh Masani

Reputation: 7504

It seems to me that your are missing UINavigationController between TVC1 and TVC2 in your storyboard. If you are using storyboards then you can create navigation item Add button type on the navigation controller itself and have a PUSH segue to TVC2. See this diagram if that makes sense. If this doesn't solve your problem then please upload example code and I will have a look.

[EDIT]

I had reproduced your issue by creating your view controllers structure in storyboard.

storyboard

If you notice here TVC1 doesn't have it's UINavigationController but it is inheriting it from VC1. Solution to your problem is rather than adding rightButton onto self add it to self.parentViewController and you will see rightButton in TVC1. But mind you it will also appear in MVC1 as it is belong to TBC1's parent. You can hide right bar button in MVC1's viewWillAppear if you don't want it there. Following is the code.

self.rightButton = [[UIBarButtonItem alloc]
                    initWithBarButtonSystemItem: UIBarButtonSystemItemAdd
                    target: self
                    action: @selector(MySelector:)];
self.rightButton.style = UIBarButtonItemStyleBordered;
self.parentViewController.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects: self.rightButton,nil];

If you want to Add right button into the TVC1's navigation controller then you need Embed TVC1 into UINavigationController. To do this, select TVC1 screen in the storyboard -> Editor -> Embed In->Navigation Controller. When you do this your code will also work and will show you right button but you will have two navigation controllers(see image below) in it because of your structure of storyboard. You will need to hide Parent's navigation controller in to the TVC1's view did load and have left button to Pop to Parentview Controller. You do the same in MVC1.

Hope this helps! Happy coding :)

output

Upvotes: 6

Related Questions