UITabBarItem.title vs. UINavigationController.title

余生长醉 提交于 2019-12-04 08:01:06

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!