2012-08-29 55 views
0

我已经为我的AppDelegate文件中的所有五个标签页设置了标题。我想在我的应用程序的另一个视图中访问该标题。我有下面的代码输出到日志窗口选定选项卡的选定索引,但我真正想要得到的是该选项卡的标题。我确实看过UITabBarController类参考,并没有看到任何可以让我这样做的东西。在UITabBarController中获取所选标签的标题

我试图避免的是某种开关或如果... else语句,其中硬编码值我已经手动设置在另一个文件中。

- (void)viewDidLoad { 
[super viewDidLoad]; 

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

} 

此代码按预期工作。这将是理想的看到标题。

回答

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

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

NSString* tabTitle = self.tabBarItem.title; 
+0

的self.tabBarItem.title给我的视图控制器不是实际的选项卡的标题。我已经设置了我的视图控制器的标题,如下所示:MyViewController * msc = [[MyViewController alloc] initwithLeftVC:rvc rightVC:dvc]; \t msc.title = @“沙拉”; – brianhevans

+0

谢谢,修复它。 – brianhevans