I allocated a UITabBarItem with a title (initWithTitle) and connected it to a UINavigationController.
I found out that if the navigation controller's root view controller has its own title, then that title permanently replaces the title specified on the tab bar item. For example, if the tab bar items' title is set to ONE and the navigation controller's root view controller's title is set to TWO, the tab bar item always shows TWO, not ONE. The only way to have the tab bar item show ONE is to omit the navigation controller's root title altogether.
The thing is, I want to have different titles for each, because the tab bar item doesn't always lead to the navigation controller's root view controller - it shows the last view controller that was pushed onto the navigation controller, which means that the root's title on the tab bar may be inappropriate. On the other hand, I can't just throw away the title in the navigation controller, because it is used on the navigation bar. Sort of Catch 22.
Is there a way around this?
I often use a navigationBar with a tabBar and almost never have the same title. The code I've used (in init
) reads:
// SET TAB BAR NAME AND IMAGE
[[self tabBarItem] setTitle:@"Short Title"];
[[self tabBarItem] setImage:[UIImage imageNamed:@"MyImage.png"]];
// SET NAVIGATION
[[self navigationItem] setTitle:@"Much Longer Title"];
I've never encountered a problem with this.
Well, here's what I found out.
It seems that if you do:
MainNavController *main = [[MainNavController alloc] init];
UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:@"ONE" image:[UIImage imageNamed:@"one.png"] tag:1];
main.tabBarItem = tabBarItem;
[tabBarItem release];
and then, in the root view controller pushed to main, you do:
self.title = @"TWO";
then what happens is what I described above.
But I can fix it if I add the following line in the root view controller of main:
self.navigationController.tabBarItem.title = @"ONE";
It seems to be sort of a "timing" issue.
The UINavigationController
is one of the viewControllers
of the UITabBarController
. Therefore, set the tabBarItem.title
of the UINavigationController
, which overrides that of the UINavigationController
's rootViewController
. The UINavigationController
sets its navigationBar
's title to its topViewController.title
.