无法将类型为“ UIViewController”的值强制转换为另一个ViewController

时间:2018-11-09 02:56:39

标签: ios swift storyboard

试图在视图控制器之间传递数据,但不断出现错误:

Could not cast value of type 'UIViewController' (0x10b4e4ca8) to 'StockList.AllListsTableViewController' (0x106d963f0).

我已经检查以确保设置了视图控制器的类,但是它仍然无法正常工作。这是我的代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let navigationController = window!.rootViewController as! UITabBarController
    let controller = navigationController.viewControllers![0] as! AllListsTableViewController
    controller.dataModel = dataModel
    return true
}

Here is my storyboard:

预先感谢

1 个答案:

答案 0 :(得分:0)

首先请确保通过Identity Inspector(AllListsTableViewController)将类名称设置为IB中的VC

第二次使用

let tab = window!.rootViewController as! UITabBarController
let navigation = tab.viewControllers![0] as! UINavigationController
let controller = navigation.viewControllers![0] as! AllListsTableViewController
controller.dataModel = dataModel