我在AppDelegate文件中设置了所有五个标签的标题。我想在我的应用程序中的另一个视图中访问该标题。我有下面的代码将选定的选项卡的选定索引输出到日志窗口,但我真正想要得到的是该选项卡的标题。我确实看过UITabBarController类参考,我没有看到任何允许我这样做的东西。
我试图避免的是某种切换或if ... else语句,其中我硬编码我已在另一个文件中手动设置的值。
- (void)viewDidLoad {
[super viewDidLoad];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"The currently selected tab has an index of: %d", appDelegate.tabBarController.selectedIndex);
}
此代码按预期工作。看到标题是理想的。
答案 0 :(得分:4)
UIViewController* vc = appDelegate.tabBarController.selectedViewController;
NSString* tabTitle = vc.tabBarItem.title;
如果代码在选定的视图控制器中,则更容易:
NSString* tabTitle = self.tabBarItem.title;