【问题标题】:Get selected tab in appdelegate在 appdelegate 中获取选定的选项卡
【发布时间】:2012-06-27 14:04:12
【问题描述】:

我想得到一个代码,我想在 appdelegate 中选择标签索引。我可以在我的视图控制器中获取它,但我如何在我的 appdelegate 中获取它?

有人可以帮我做这件事吗?

提前致谢。

【问题讨论】:

标签: ios5 xcode4


【解决方案1】:

您可以使用 MyTabBar.selectedIndex 并将 Object 放入 AppDelegateClass.. 或者,如果您已经有 TabBar 可用 你可以使用

AppDelegate *app = [[UIApplication sharedApplication] delegate];
NSLog(@"%i",app.Tab.selectedIndex);

如果您需要在同一个课程中获取它以便您可以使用

NSLog(@"%i",self.Tab.selectedIndex);

如果您想阻止用户转到另一个选项卡,您可以使用委托

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    if (tabBarController.selectedIndex == 3) {
            //if the user will select tab  3 so user will not go to it 
        return NO;
    }else{
            // if any other tab so return yse will let you to other tabs
        return YES;
    }

}

【讨论】:

  • 在您想要在其中获取选定选项卡的类中.. 只需导入您的 applicationdelegate 类并使用此代码访问选定的选项卡
  • 但是,伙计,如果我想在 appdelegate 类中获得它,有可能吗?原因实际上是希望用户在某些情况下阻止查看视图控制器。
  • @SoumalyaBanerjee - 我在上面链接了一个问题,你可以检查一下。
  • 我已经编辑了我的答案,这样你就可以防止用户根据条件切换标签了..
猜你喜欢
  • 2023-03-22
  • 2019-11-30
  • 2021-12-22
  • 2012-08-24
  • 1970-01-01
  • 1970-01-01
  • 2017-12-31
  • 2011-05-14
  • 1970-01-01
相关资源
最近更新 更多