【问题标题】:Get title of selected tab in UITabBarController获取 UITabBarController 中选定选项卡的标题
【发布时间】:2012-08-24 08:10:26
【问题描述】:

我已在我的 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);

}

此代码按预期工作。看到标题就好了。

【问题讨论】:

    标签: objective-c ios5 uitabbarcontroller appdelegate


    【解决方案1】:
    UIViewController* vc = appDelegate.tabBarController.selectedViewController;
    NSString* tabTitle = vc.tabBarItem.title;
    

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

    NSString* tabTitle = self.tabBarItem.title;
    

    【讨论】:

    • self.tabBarItem.title 给了我视图控制器的标题,而不是实际的选项卡。我为标签设置了视图控制器的标题,如下所示: MyViewController *msc = [[MyViewController alloc] initwithLeftVC:rvc rightVC:dvc]; msc.title=@"沙拉";
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多