我有一个需要凭据的登录屏幕,根据验证结果,用户是否被重定向到另一个 View Controller 。登录 View Controller 由 NavigationController 控制,一旦登录成功,用户将被重定向到由 UITabBarController
控制的 Home View Controller 。
当我触发 segue 时,它显示以下错误:
Could not cast value of type 'UITabBarController' (0x10d414030) to '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
,您需要将其转换为 UITabBarController
并使用 viewControllers
属性获取 ViewController 对象标签栏 Controller 。
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
关于ios - Swift - 无法转换类型为 'UITabBarController' 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34120068/