ios - Swift - 无法转换类型为 'UITabBarController' 的值

标签 ios swift uitabbarcontroller

我有一个需要凭据的登录屏幕,根据验证结果,用户是否被重定向到另一个 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/

相关文章:

android - 从 Apple Store 安装的应用程序会自动创建主屏幕快捷方式图标吗?

objective-c - 访问已发布的 NSString 不会使应用程序崩溃

ios - 如何用动画结束 UILongPressGestureRecognizer?

swift - 如何根据信标的接近程度刷新背景图像?

ios - 将 NSNotification 发布到新的 ViewController

ios - 将数据传递给下一个 ViewController 时出错

ios - 在 Swift 的另一个类中使用一个类中的函数

ios - 放松 segue 不起作用

iphone - 横向模式下的 UINavigationController 导航堆栈问题

ios - 如何将一组新的 View Controller 从一个选项卡推送到另一个选项卡?