0
votes

I have a project which uses a tab bar. All the tabs in the tab bar have the same main navigation controller. On one of the views I want to have custom buttons which the user can use.

I am having problems adding these buttons to the storyboard and having them shown:

  1. Programatically adding them

My project is written in Objective-C so I tried adding them programatically to my viewController:

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

I also tried adding them using the following:

self.navigationController.navigationItem.rightBarButton = ...

As the initial method didn't work. I didn't know if I needed a pointer to the tab bar navigation controller to add a button. Neither of these approaches worked.

  1. Adding the buttons in the storyboard

I added a navigation item to my viewController in the storyboard and then dragged my buttons into it. You can see a screenshot below:

enter image description here

The problem with this technique was that the buttons didn't appear when I loaded the code. Once again I thought that this might be to do with the UITabBar navigation controller overwriting the storyboard.

  1. Add a navigation controller to the storyboard to load the viewController

I next tried adding a UINavigationController and then loading my viewController as the root view. The root view still containing the navigation item and the buttons.

enter image description here

This was the first method that seemed to have any effect on the functionality I was after. The weird thing was that the navigation item was added below the navigation bar:

enter image description here

As you can see it has added the navigation items below the navigation bar instead of inside it. This functionality works as hoped except for the buttons being in the wrong place.

  1. Other things I have tried:

-Dragging the UIBarbuttonItems into the navigation controller nav bar in the storyboard

Conclusion: To me this seems like it should be an easy thing to achieve with storyboards. I can't understand what is causing this code to not work correctly and why Apple would enable functionality that adds the navigation items under a nav bar without me adding a navigation bar into the same view in the storyboard.

If anyone can give me advice on using storyboards and how to add navigation buttons to a view controller when using a tab bar that would be very appreciated.

1
"All the tabs in the tab bar have the same main navigation controller" Okay, we're only on the second sentence of the question and already this makes no sense. I take it you mean this a tab bar controller with several child view controllers? But then how can they possibly all have "the same navigation controller"?matt
@matt we have a Main Navigation Controller which loads a view controller which is a UITabBarController. When I say "they all have the same navigation controller" I mean that each view doesn't have it's own navigation controller added in the storyboard. Instead we have viewControllers loaded into the UITabBar. I assumed this means they all share the same navigation controller.sam_smith
If I understand you correctly, what this means is that you have a tab bar controller inside a navigation controller. That means the navigation controller has just one child that "shares" it, namely, the tab bar controller. The tab bar's children are not consulted as to the state of the navigation controller.matt
Well that's why what you're trying to do doesn't work. The tab bar controller's children are not "in" a navigation interface (even though their grandfather is a navigation controller). The tab bar controller is. Tabbing from one view controller to another will have no effect whatever on the navigation controller, its navigation bar, etc.matt
I've no idea what you've done to make it "work", so you should take the credit and answer your own question! (Perfectly legal on Stack Overflow; in a couple of days you can even accept your own answer.)matt

1 Answers

1
votes

Huge thanks to @matt for helping me think this issue through.

This was an interesting case where I needed to think slightly harder on what was actually happening to be able to find the answer - in this case needing a bit of help in the comments.

The problem was that I wasn't setting the navigation item in the correct place. The framework of my app was set up as follows:

tabBarController -> viewController -> navigationBar

My initial approach was to try to set the navigation item on the viewController. As you can see in the framework above there is no navigation item to set. This is why self.navigationController wasn't working.

Next I attempted to add a navigationController in to remedy this:

tabBarController -> navigationController -> viewController -> navigationBar

This didn't work either as the navigation bar is being set in the tabBarController first and so the navigationController won't override it.

This step was the hole in my logic. I was assuming something was wrong with my code/storyboard somewhere as the navigationControllers and items I was setting weren't appearing.

The issue was that I was not understanding the problem. In this case we have the tabBarController which controls the tabBar. This controls the navigation bar. This means if I want to access it I need to get the current navigation controller for our parent view. This is done easily with self.tabBarController:

The solution:

self.tabBarController.navigationItem.rightBarButtonItem = ...;

As you can see this is very similar to my attempts in the above question. Both follow the conventions of navigation items. The difference was that in the solution I was accessing the parent navigationController of the entire view.