mrd mrd - 1 year ago 86
iOS Question

How to set the navigation bar title from view controllers inside tabs?

I have a tab bar controller embedded in a navigation controller.
I want to set the title in the navigation from the view controllers inside the tabs.

If I use:

self.title = "ABC";


self.tabBarItem.title = "ABC";

the title of the tab at the bottom is set to "ABC", but not the title in the navigation bar at the top.

However, I only want to set the title in the navigation bar, not the tabs at the bottom.

How can I do that?

enter image description here

Answer Source

You have to pass the navigationItem up the chain.

The UINavigationController shows the navigationItem belonging to its topViewController which is UITabBarController.

The UITabBarController shows the navigationItem titles in its tabs. So what you need to do is make sure that the tabBarController's navigationItem is it's selectedViewController's navigationItem

So to recap:

UINavigationController title = topViewController.navigationItem.title

UITabBarController tabTitle = selectedViewController.navigationItem.title

UIViewController title = navigationItem.title
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download