Swift – 无法转换’UITabBarController’类型的值
我有一个获取凭据的登录屏幕,并根据validation结果,用户被重定向到另一个视图控制器。 登录视图控制器由NavigationController控制,一旦登录成功,用户将被重定向到由UITabBarController
控制的Home视图控制器。
当我触发segue时,它会显示以下错误:
无法将’UITabBarController’类型的值(0x10d414030)转换为’Mawq.HomeViewController’(0x10a7ed220)。
以下是segue的代码:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { if (segue.identifier == "showHomeFromLoginSegue") { /*Provide ServiceToken a value from API*/ // pass data to next view let destinationVC = segue.destinationViewController as! HomeViewController destinationVC.userObject = self.userObject; } }
知道如何克服这个问题吗?
由于您的segue连接到UITabBarController
,您需要将其viewControllers
为UITabBarController
并使用选项卡栏控制器的viewControllers
属性获取ViewController对象。
let tabCtrl = segue.destinationViewController as! UITabBarController let destinationVC = tabCtrl.viewControllers![0] as! HomeViewController // Assuming home view controller is in the first tab, else update the array index destinationVC.userObject = self.userObject; For Swift 4: let tabCtrl: UITabBarController = segue.destination as! UITabBarController let destinationVC = tabCtrl.viewControllers![0] as! HomeViewController destinationVC.userObject = userObject[String!] // In case you are using an array or something else in the object