iOS以编程方式更改标签

时间:2012-08-27 10:38:48

标签: iphone ios ios4

我正在开发一个带有标签栏和导航的iOS应用程序。当我按下第一个选项卡中的按钮时,我希望它使用导航控制器加载第二个选项卡并按下下一个视图。有人可以帮忙吗?

4 个答案:

答案 0 :(得分:3)

您可以通过

以编程方式选择制表符
self.tabBarController.selectedIndex=1;

答案 1 :(得分:1)

设置selectedViewController:

self.tabBarController.selectedViewController = viewControllerYouWant;

例如,

self.tabBarController.selectedViewController 
    = [self.tabBarController.viewControllers objectAtIndex:2];

答案 2 :(得分:1)

第一个选项卡中的按钮向选项卡控制器发送一条消息,告诉它选择第二个选项卡。然后,您将消息发送到第二个选项卡的rootView,该选项卡是导航控制器,并告诉它使用第一个按钮推送您在类中创建的此类对象。

答案 3 :(得分:1)

这是一个Swift解决方案,适合任何需要它的人。我必须做同样的事情,只需从选项卡2中的嵌套视图控制器转到选项卡4中的另一个嵌套视图控制器。以下是我实现的方法:

func goToHelpViewController(){
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        let desiredIndex:Int = 3
        self.tabBarController?.selectedIndex = desiredIndex
        let settingsStoryBoard = UIStoryboard(name: "SettingsSection", bundle: nil)
        let helpViewController = settingsStoryBoard.instantiateViewControllerWithIdentifier("HelpViewController") as! HelpViewController
        let settingsRootNavigationController = self.tabBarController?.viewControllers![desiredIndex] as! UINavigationController

        settingsRootNavigationController.popToRootViewControllerAnimated(false)
        settingsRootNavigationController.pushViewController(helpViewController, animated: true)
    })
}

Swift 3.0

func goToHelpViewController(){
    DispatchQueue.main.async(execute: { () -> Void in
        let desiredIndex:Int = 1
        self.tabBarController?.selectedIndex = desiredIndex
        let settingsStoryBoard = UIStoryboard(name: "SettingsSection", bundle: nil)
        let helpViewController = settingsStoryBoard.instantiateViewController(withIdentifier: "HelpViewController") as! HelpViewController
        let settingsRootNavigationController = self.tabBarController?.viewControllers![desiredIndex] as! UINavigationController

        settingsRootNavigationController.popToRootViewController(animated: false)
        settingsRootNavigationController.pushViewController(helpViewController, animated: true)
    })
}