androniennn
androniennn

Reputation: 3137

Can't do ANYTHING with navigation bar

I have this structure:

storyboard screenshot

When I want to add a ´UINavigationItem´ from the storyboard the navigation bar is "disabled", so I tried to add the right button programatically:

UIBarButtonItem *button = [[UIBarButtonItem alloc]
                           initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                           target:self
                           action:@selector(showPickerView:)];
self.navigationItem.rightBarButtonItem = button;

But nothing happens. I renamed the navigation bar title from storyboard, but when I run the app the title is not set. I really don't know what is the source of the problem. There is just the back button that is appearing. Thank you for your help.

Upvotes: 1

Views: 5395

Answers (4)

Brian Ogden
Brian Ogden

Reputation: 19222

As other answers have pointed out, the hierarchy of your UINavigationViewController is incorrect. The UITabBarViewController will always be the parent of your UINavigationViewControllers.

For each UIViewController that is a "Tab", you need your UINavigationViewController to be "in front" of each "tab" UIViewConrolller that will have segues.

So you do not need a UINavigationViewController for every "tab" UIViewController, as someone else pointed out on this thread, only for "tabs" that will segue to other Views that you want to track navigation with a UINavigationViewController

The hierarchy can be seen in @App Dev Guy's answer here

In Swift 2, Xcode 7 has a very handy feature for adding a UINavigationController:

  1. Select the UIViewController that is being used as a "tab" for the UITabBarNavigationController
  2. On the top Xcode menu, select "Editor" ->
  3. "Embed In" ->
  4. "Navigation Controller"

    enter image description here

Upvotes: 1

Wain
Wain

Reputation: 119031

Each item in the tab bar controller should have a navigation controller as the root controller (well, you don't need all of them to have a nav controller if you don't need them). What you currently have is a tab bar controller in the navigation controller (unless it's modal) so the view controllers contained in the tab bar controller can't see out to the navigation controller.

Upvotes: 2

user2535467
user2535467

Reputation:

Try it like this. It's a little different than how you are doing it and it works for me:

UIBarButtonItem *button = [[UIBarButtonItem alloc] 
                           initWithTitle:@"title"                                            
                           style:UIBarButtonItemStyleBordered 
                           target:self 
                           action:@selector(showPickerView:)];
self.navigationItem.rightBarButtonItem = button;

If you want to do this in the Storyboard, you don't drag the "Navigation Item", but the "Bar Button Item" instead.

Upvotes: 0

Rob van der Veer
Rob van der Veer

Reputation: 1148

I tried to reproduce your problem.

  1. I started a new 'Single View' project.
  2. Selected the iPhone storyboard
  3. Selected the view controller
  4. Menu -> Editor -> Embed in -> Navigation Controller.
  5. Open the object browser
  6. type 'Bar Button Item' in the search bar
  7. draw the bar button item over the navigation bar.

Done. See if you can do the same.

enter image description here

Upvotes: 0

Related Questions