在UITabBarController中获取所选选项卡的标题 [英] Get title of selected tab in 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.
我想避免的是某种切换或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屋!