mrd
mrd

Reputation: 4699

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";

or

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

Upvotes: 0

Views: 2517

Answers (3)

tryp
tryp

Reputation: 1120

You are probably using the wrong element, try with the following example :

self.tabBarController.title = @"YOUR_TITLE";

Upvotes: 0

figgleforth
figgleforth

Reputation: 450

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

Upvotes: 2

user2265763
user2265763

Reputation:

You can set it using self.navigationItem.title.

self.navigationItem.title=@"ABC";

Upvotes: 3

Related Questions