目标C:如何通过程序从一个标签栏切换到另一个标签栏

Zhe*_*hen 24 objective-c uitabbarcontroller ios

我的tabbar控制器中有5个不同的选项卡.

我的目的是能够通过代码从一个标签栏切换.例如,我目前处于应用程序的第5个选项卡中,当我单击"完成"按钮时,应用程序应将我的视图切换到属于第1个选项卡的根视图控制器.

有关如何做到这一点的任何建议?

Jha*_*iya 45

设置selectedViewController属性UITabBarController:

self.myTabBarController.selectedViewController = myViewController;
Run Code Online (Sandbox Code Playgroud)

使用方法如下

self.myTabBarController.selectedViewController 
    = [self.myTabBarController.viewControllers objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)


Cob*_*ink 10

下面是一个更简单的答案(如果您知道Tab Bar项的索引不在"more"视图控制器中):只需获取tabController的引用并设置"selectedIndex"属性

self.tabBarController.selectedIndex = 0;
Run Code Online (Sandbox Code Playgroud)

参考:https://developer.apple.com/library/ios/documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html#jumpTo_6