如何从UIViewController中选择TabBarController中的特定选项卡哪个不是选项卡之一?

时间:2013-02-18 03:10:29

标签: uiviewcontroller uitabbarcontroller

我花了一个小时的时间来研究这个问题,尽管搜索过,但我无法弄明白。基本上,我有一个TabBarController(TBC),其中有几个选项卡,每个选项卡都有一个关联的UIViewController。在我的应用程序中,我 也有 其他UIViewControllers,它们不属于TBC的所有部分。我在其中一个非TBC UIViewControllers上有一个按钮,当点击它时,不仅应该导航到TBC,还应该导航到非0的特定选项卡索引。

我发现很多人都说,“哦,只是引用self.tabBarController ......等等等等等等,但当我在我的非TBC UIViewControllers之一时,self对我不起作用。

我确实找到了以下我认为可能有效的但却没有的。代码获取对我需要的TBC的引用,在预期显示时指定非零索引,然后执行segue。不幸的是,我继续只看到segue之后的第一个标签。

// doesn't work...    
UITabBarController *TBC = [self.storyboard instantiateViewControllerWithIdentifier:@"TBC"];
TBC.selectedIndex = 1;
[self performSegueWithIdentifier:@"goTBC" sender:nil];

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我最终通过在AppDelegate中创建一个int变量来解决这个问题,以跟踪应该显示哪个选项卡索引,默认为0.然后,在我的TabBarController的viewWillAppear方法中,我将selectedIndex设置为我存储的选项卡索引AppDelegate中。这样可以解决问题,尽管我认为它不是最干净的。

- (void)viewWillAppear:(BOOL)animated
{
     // Assume ad is your app delegate...
     // Assume navToIndex is the int I referenced above in app delegate
     self.selectedIndex = ad.navToIndex;
}