Swift - 如何使用从 ViewController 到特定 TabBarController 的按钮

Swift - How do I segue with a button from ViewController to a specific TabBarController

从 Swift 中 TabBarController 之前的 ViewController,我想发送到 TabBarController 之后的特定 Tab,它可能是 NavigationController 或简单的 ViewController。

目前我是这样制作的:

@IBAction func settingsButtonPressed(sender: AnyObject) {
        tabBarController?.selectedIndex = 3 // 4d tab
        performSegueWithIdentifier("tabBarShow", sender: self)
}

通过此操作,它会执行 segue,但它始终显示第一个选项卡。

感谢您的帮助!

您可以尝试设置 selectedViewController 属性 而不是 selectedIndex:

tabBarController.selectedViewController = tabBarController.viewControllers![2]

这个人有同样的问题:

编辑 这是假设 tabBarController 已经初始化,如果没有,你需要先初始化它。

您是在加载 TabbarController 之前设置选定的索引,因此请将选定的索引存储在某处,并将其设置在 TabBarViewController 的 ViewDidLoad 中。

self.selectedIndex = valueOFSelectedIndexStored;

您可以使用:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "tabBarShow"){
        if let tabVC = segue.destinationViewController as? UITabBarController{
            tabVC.selectedIndex = 3
        }
    }
}