UITabBarItem.title 与 UINavigationController.title

发布于 2024-11-19 17:48:36 字数 402 浏览 2 评论 0原文

我分配了一个带有标题 (initWithTitle) 的 UITabBarItem 并将其连接到 UINavigationController。

我发现,如果导航控制器的根视图控制器有自己的标题,那么该标题将永久替换选项卡栏项目上指定的标题。例如,如果选项卡栏项目的标题设置为“一”,并且导航控制器的根视图控制器的标题设置为“二”,则选项卡栏项目始终显示“二”,而不是“一”。让选项卡栏项目显示 ONE 的唯一方法是完全省略导航控制器的根标题。

问题是,我希望每个标题都有不同的标题,因为选项卡栏项目并不总是导致导航控制器的根视图控制器 - 它显示了被推送到导航控制器上的最后一个视图控制器,这意味着根的标签栏上的标题可能不合适。另一方面,我不能直接丢弃导航控制器中的标题,因为它用在导航栏上。有点像第 22 条军规。

有办法解决这个问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

七七 2024-11-26 17:48:36

我经常将导航栏与选项卡栏一起使用,并且几乎从不具有相同的标题。我使用的代码(在 init 中)显示:

// 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 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.

各空 2024-11-26 17:48:36

嗯,这就是我发现的。

似乎如果你这样做:

MainNavController *main = [[MainNavController alloc] init];
UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:@"ONE" image:[UIImage imageNamed:@"one.png"] tag:1];
main.tabBarItem = tabBarItem;
[tabBarItem release];

然后,在推送到主视图的根视图控制器中,你会这样做:

self.title = @"TWO";

然后发生的事情就是我上面描述的。

但如果我在 main 的根视图控制器中添加以下行,我可以修复它:

self.navigationController.tabBarItem.title = @"ONE";

这似乎是一个“计时”问题。

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.

蓝海似她心 2024-11-26 17:48:36

UINavigationControllerUITabBarControllerviewControllers 之一。因此,设置 UINavigationControllertabBarItem.title,它会覆盖 UINavigationControllerrootViewController 的值。 UINavigationController 将其 navigationBar 的标题设置为其 topViewController.title

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.

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