切换选项卡栏在Swift中以编程方式失败

时间:2018-03-09 23:10:46

标签: ios swift uitabbarcontroller

我想切换tabbar项目2,但它总是转到第1项中的日志,它表示它也会转到第2项,但它显示第1项。我正在使用segue。 viewControllers![1]

if segue.identifier == Constants.Identifiers.goToWinTabFromSearchMatch {
        let tabBarController = segue.destination as! UITabBarController
        let navController = tabBarController.viewControllers![1] as! UINavigationController
        let controller = navController.topViewController as! WinViewController
    }

 @IBAction func doneToBackToWinTab(_ sender: Any) {
    print("i wana back to the win tab")
    performSegue(withIdentifier: Constants.Identifiers.goToWinTabFromSearchMatch, sender: nil)


}

1 个答案:

答案 0 :(得分:1)

我也对你的代码片段感到困惑,因为它只是声明了一堆常量,你不会在这里执行任何操作。要更改UITabViewController中的标签,只需使用selectedIndex属性(这是一个简单的Int)。将其设置为有效值并vo。

但是,您的if块中有很多关于选项的问题。强制解包和访问可能甚至不存在的数组字段只是在寻找麻烦。我强烈建议您以正确的方式进行,并通过if let正确打开它们。

即,你的tabBarController.viewControllers![1] as! UINavigationController实际上就像在明火旁边放一罐煤油 - 你不知道什么时候会最终爆炸。

相关问题