1

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.

androniennn
  • 3,117
  • 11
  • 50
  • 107

4 Answers4

2

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.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Ahh okay, so in every controller of the TabBarController I should have navigation controller. Okay, okay. – androniennn Jul 17 '13 at 17:06
  • I just made all things like you indicated, however, now I have 2 navigation bars. – androniennn Jul 17 '13 at 17:08
  • 2
    Remove the "root" navigation controller. If you use a TabBarController it should always be the topmost controller. Otherwise you confuse every single user of your app. – Matthias Bauch Jul 17 '13 at 17:10
  • @MatthiasBauch: Do you mean the first navigation controller in the top left? – androniennn Jul 17 '13 at 17:11
  • 1
    Yes, if the tab controller isn't presented modally then having that 'outer' nav controller makes little sense. – Wain Jul 17 '13 at 17:12
  • So if I want to have a navigation bar in the FIRST view controller(top left) I should add it manually? and then the transition from that controller to the tab bar controller should be modal (cause I don't have a uinavigationcontroller to push). ? and to go back to the first controller I should make it programatically. – androniennn Jul 17 '13 at 17:15
  • 2
    You should probably replace your tabBarController with something else. In my opinion a tabBar must be top in a view hierarchy. Everything else does not make much sense and it confuses your users. A navigationController is always secondary to a tabBarController. I have not seen a single (successful) app that uses a hierarchy like you intent to do. I would replace your tabBarController with a tableView which has 3 rows. Or a UIPageViewController with 3 pages. Or a custom viewController with 3 buttons. Or put a segmentedControl with 3 segments in the navigationBar. Everything, but not a tabBar. – Matthias Bauch Jul 17 '13 at 17:38
  • But that's just some unsolicited UI advice, feel free to ignore it :-) – Matthias Bauch Jul 17 '13 at 17:43
  • @MatthiasBauch: I'll absolutely take your advice. Thank you very very much. – androniennn Jul 17 '13 at 18:17
1

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

Community
  • 1
  • 1
Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
0

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.

CaptJak
  • 3,592
  • 1
  • 29
  • 50
0

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

Rob van der Veer
  • 1,148
  • 1
  • 7
  • 20
  • The problem is not in the first controller of the navigation controller, but in the controllers of the TABarController. – androniennn Jul 17 '13 at 17:01
  • To add items to a tab bar, you need to a tab bar items. Not bar button items. – Rob van der Veer Jul 17 '13 at 17:03
  • @androniennn, i now understand your problem, i've reproduced it and i also see no titles or navigation bar buttons after the tab bar controller. And if I add the navigation controllers to the tab items, you get two nice navigation bars. The top bar is from the parent view, and the second bar is from the tab bar item. I guess this stuff is by design. – Rob van der Veer Jul 17 '13 at 17:11