Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITabBarItem.title vs. UINavigationController.title

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?

like image 760
Amiram Stark Avatar asked Jul 10 '11 23:07

Amiram Stark


2 Answers

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.

like image 198
PengOne Avatar answered Sep 21 '22 12:09

PengOne


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.

like image 27
Amiram Stark Avatar answered Sep 19 '22 12:09

Amiram Stark