设置独立于 TabBar 标题的 NavController 标题
我正在完成以编程方式构建视图控制器及其关联的导航控制器的相当典型的过程,然后将导航控制器添加到 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]];
这工作正常,NavigationController 标题和 TabBar 标题将显示“First”。现在,我想将导航控制器标题更改为“FooBar”,并将选项卡栏标题保留为“First”。
我试过了:
firstViewNav.navigationItem.title = @"FooBar";
但这似乎并没有改变它。如果我更改由该导航控制器管理的实际 ViewController (firstView.title) 上的标题,则 TabBar 标题和导航栏标题都会更改,这是不希望的。
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如此处回答的: self.title 设置 navigationController 和 tabBarItem 的标题?为什么?
As answered here: self.title sets navigationController and tabBarItem's title? Why?
将选项卡栏项添加到导航控制器,并将选项卡栏项的标题设置为您希望选项卡栏标签读取的内容。我通常在 NIB 中执行此操作,但如果您以编程方式执行此操作,它应该可以工作。
将导航项的标题设置为您想要在导航栏文本中显示的内容。您还可以在视图出现时更改此文本,例如,如果您希望更改它。
正如您所发现的,不要设置视图控制器的标题,因为这将覆盖其他两个值。
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.
只需隐藏导航栏并添加具有所需标题的工具栏即可。
just hide the NavigationBar and add a toolbar with the title you want.