在 UITabBarController 中获取所选选项卡的标题 [英] Get title of selected tab in UITabBarController

查看:40
本文介绍了在 UITabBarController 中获取所选选项卡的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在 AppDelegate 文件中为所有五个选项卡设置了标题.我想在我的应用程序的另一个视图中访问该标题.我有下面的代码将所选选项卡的所选索引输出到日志窗口,但我真正想要的是该选项卡的标题.我确实查看了 UITabBarController 类参考,但没有看到任何允许我这样做的内容.

I have set the title for all five of my tabs in my AppDelegate file. I would like to access that title in another view within my application. I have the code below to output to the log window the selected index of that selected tab but what I'd really like to get is the title for that tab. I did have a look through the UITabBarController Class Reference and I didn't see anything that would allow me to do this.

我试图避免的是某种 switch 或 if...else 语句,其中我对已经在另一个文件中手动设置的值进行了硬编码.

What I am trying to avoid is some sort of switch or if...else statement where I hardcode values I have already manually set in another file.

- (void)viewDidLoad {
[super viewDidLoad];

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"The currently selected tab has an index of: %d", appDelegate.tabBarController.selectedIndex);

}

此代码按预期工作.最好能看到标题.

This code works as intended. It would just be ideal to see the title.

推荐答案

UIViewController* vc = appDelegate.tabBarController.selectedViewController;
NSString* tabTitle = vc.tabBarItem.title;

如果代码在选定的视图控制器中,那就更简单了:

If the code is in the selected view controller it's even easier:

NSString* tabTitle = self.tabBarItem.title;

这篇关于在 UITabBarController 中获取所选选项卡的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆