设置独立于 TabBar 标题的 NavController 标题

发布于 2024-12-07 02:14:20 字数 939 浏览 0 评论 0原文

我正在完成以编程方式构建视图控制器及其关联的导航控制器的相当典型的过程,然后将导航控制器添加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

[旋木] 2024-12-14 02:14:20

正如此处回答的: self.title 设置 navigationController 和 tabBarItem 的标题?为什么?

self.title = @"Title for TabBarItem"; // TabBarItem.title inherits the viewController's self.title
self.navigationItem.title = @"Title for NavigationBar";

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";
作死小能手 2024-12-14 02:14:20

将选项卡栏项添加到导航控制器,并将选项卡栏项的标题设置为您希望选项卡栏标签读取的内容。我通常在 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.

混浊又暗下来 2024-12-14 02:14:20

只需隐藏导航栏并添加具有所需标题的工具栏即可。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文