Jason Vasquez
Jason Vasquez

Reputation: 270

Set NavController title independently of TabBar title

I'm working through the fairly typical process of building view controllers and their associated navigation controllers programmatically, and then adding the navigation controllers to a tabBarController.

self.tabBarController = [[[UITabBarController alloc] init] autorelease];
[self.tabBarController setHidesBottomBarWhenPushed: YES];

FirstViewController * firstView = [[[FirstViewController alloc] init] autorelease];
firstView.title = @"First";
firstView.tabBarItem.image = [UIImage imageNamed:@"icon_first_view.png"];
UINavigationController * firstViewNav = [[[UINavigationController alloc] initWithRootViewController:firstView] autorelease];

[tabBarController setViewControllers:[NSArray arrayWithObjects: firstViewNav, nil]];

That works fine, and the NavigationController title and the TabBar title will say "First". Now, I would like to change the Navigation Controller title to say "FooBar", and leave the tab bar title as "First".

I've tried:

firstViewNav.navigationItem.title = @"FooBar";

But that doesn't seem to change it. If I change the title on the actual ViewController managed by this nav controller (firstView.title), then both the TabBar title and the Navbar title both change, which is not desired.

Any ideas?

Upvotes: 4

Views: 1169

Answers (3)

geon
geon

Reputation: 8470

As answered here: self.title sets navigationController and tabBarItem's title? Why?

self.title = @"Title for TabBarItem"; // TabBarItem.title inherits the viewController's self.title
self.navigationItem.title = @"Title for NavigationBar";

Upvotes: 3

AndrewS
AndrewS

Reputation: 8472

Add a Tab Bar Item to the navigation controller, and set the title of the TabBarItem to what you want the tab bar label to read. I normally do this in the NIB but it should work if you do it programmatically.

Set the title of the navigation item to what you want in the text of the nav bar. You can also change this text when the view will appear, for example, if you want it to change.

As you discovered, do NOT set the title of the view controller, as that will override both other values.

Upvotes: 0

Shrey
Shrey

Reputation: 1977

just hide the NavigationBar and add a toolbar with the title you want.

Upvotes: -1

Related Questions