Swift-无法转换类型为'UITabBarController'的值 [英] Swift - Could not cast value of type 'UITabBarController'

查看:60
本文介绍了Swift-无法转换类型为'UITabBarController'的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录屏幕,该屏幕需要凭据,并且根据验证结果,是否将用户重定向到另一个视图控制器.登录视图控制器由NavigationController控制,一旦登录成功,用户将被重定向到由 UITabBarController 控制的主视图控制器.

I have a login screen that takes credentials, and based on outcome of verification, user is redirected to another view controller or not. The login view controller is controlled by NavigationController and once the login is successful, user is redirected to a Home view controller controlled by UITabBarController.

当我触发segue时,它显示以下错误:

When I trigger the segue, it says the following error:

无法将类型为'UITabBarController'(0x10d414030)的值强制转换为'Mawq.HomeViewController'(0x10a7ed220).

Could not cast value of type 'UITabBarController' (0x10d414030) to 'Mawq.HomeViewController' (0x10a7ed220).

以下是segue的代码:

Here is the code for the 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;

        }
    }

有什么办法解决这个问题吗?

Any idea how to overcome this issue?

推荐答案

由于您的segue已连接到 UITabBarController ,因此您需要将其强制转换为 UITabBarController 并获取使用选项卡栏控制器的 viewControllers 属性的ViewController对象.

Since your segue is connected to a UITabBarController, you need to cast it to UITabBarController and get the ViewController object using the viewControllers property of the tab bar 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

这篇关于Swift-无法转换类型为'UITabBarController'的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆