【问题标题】:How to get the selected index of my tabbarcontroller inside the related viewcontroller?如何在相关视图控制器中获取我的 tabbarcontroller 的选定索引?
【发布时间】:2012-10-19 14:51:03
【问题描述】:

大家好,我想在相关的UIViewControllers 中获取我的UITabbarController 的选定索引。 我试图用[self parentViewController]; 获得parentViewController 但似乎,这不是正确的方法,因为我得到的控制器没有像selectedIndex 这样的方法。有什么建议吗?

【问题讨论】:

  • 我找到了解决方案:NSInteger selectedIndex = self.tabBarController.selectedIndex;

标签: iphone objective-c xcode macos cocoa


【解决方案1】:

访问 tabBarController 的 selectedIndex 属性

tabBarController.selectedIndex

【讨论】:

    【解决方案2】:
        int i = [self.tabBarController selectedIndex];
        NSLog(@"Selected Index = %d",i);
    

    【讨论】:

    • 如果我选择索引 0 处的选项卡,我得到的数字是 2147483647。它是否必须使用正确的格式?
    • 这里当您当时单击任何选项卡时,它会返回先前的 viewcontroler 标记值...例如,如果您在 2 个选项卡中并且您当时在上面的代码中选择了 1 个选项卡,则返回 Selected index = 2..
    【解决方案3】:

    这很容易做到。假设您在TabBarcontroller 中有 4 个图标。如果您想访问其中任何一个,您必须执行以下操作:

       [self.tabBarController setSelectedIndex:0];
       [self.tabBarController setSelectedIndex:1];
       [self.tabBarController setSelectedIndex:2];
       [self.tabBarController setSelectedIndex:3];
    

    索引是tabBar中的位置

    【讨论】:

      猜你喜欢
      • 2014-05-25
      • 2012-02-11
      • 2011-01-26
      • 1970-01-01
      • 2020-08-29
      • 2014-01-27
      • 2016-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多