4

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?

Jason Vasquez
  • 270
  • 1
  • 11

3 Answers3

3

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";
Community
  • 1
  • 1
geon
  • 8,128
  • 3
  • 34
  • 41
0

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.

AndrewS
  • 8,196
  • 5
  • 39
  • 53
-1

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

Shrey
  • 1,959
  • 2
  • 21
  • 44
  • Thanks, that actually works nicely. It's unfortunate the default behavior there isn't more intuitive. – Jason Vasquez Sep 29 '11 at 17:42
  • 1
    A much better solution: http://stackoverflow.com/questions/1540718/self-title-sets-navigationcontroller-and-tabbaritems-title-why – geon Oct 18 '13 at 07:08