我有一个获取凭据的登录屏幕,并根据验证结果,用户被重定向到另一个视图控制器.登录视图控制器由NavigationController控制,一旦登录成功,用户将被重定向到受其控制的Home视图控制器UITabBarController
.
当我触发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已连接到a UITabBarController
,因此您需要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