2

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

我要避免的是某种 switch 或 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);

}

此代码按预期工作。看到标题就很理想了。

4

1 回答 1

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

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

NSString* tabTitle = self.tabBarItem.title;
于 2012-08-29T17:24:13.510 回答