简体   繁体   English

Swift-无法将'UINavigationController'类型的值强制转换为'MapViewController'

[英]Swift - Could not cast value of type 'UINavigationController' to 'MapViewController'

I'm currently trying to convert my Swift iOS app to Swift 2. I've managed to remove the compiler errors (with some great help from stackoverflow) but I'm running into issues with the app once it's compiled now. 我目前正在尝试将我的Swift iOS应用程序转换为Swift2。我已经设法消除了编译器错误(在stackoverflow的帮助下),但是一旦编译后,我就遇到了问题。 My app has a tabBarController with 3 tabs. 我的应用程序有一个带有3个选项卡的tabBarController。 When I select the 2nd tab, the app now crashes with a 'SIGABRT' error. 当我选择第二个选项卡时,应用程序现在崩溃,并显示“ SIGABRT”错误。

I get the error: 我得到错误:

Could not cast value of type 'UINavigationController' (0x106fbd588) to 'FlightTracker.MapViewController' (0x105722430) 无法将类型'UINavigationController'(0x106fbd588)的值强制转换为'FlightTracker.MapViewController'(0x105722430)

For the following code: 对于以下代码:

let mapviewcontroller = self.tabBarController?.viewControllers?[0] as! MapViewController

Why can it not cast the controller as a MapViewController? 为什么不能将控制器转换为MapViewController? I had it working this way prior to iOS 9 & Swift 2. 在iOS 9和Swift 2之前,我曾以这种方式工作过。

It should work 它应该工作

let navigationController = self.tabBarController?.viewControllers?[0] as! UINavigationController //Check which is your tab on your storyboard, 0, 1, n ?

let mapviewcontroller = navigationController.viewControllers[0] as! MapViewController

self.tabBarController?.viewControllers?[0] => it's your UINavigationController self.tabBarController?.viewControllers?[0] =>是您的UINavigationController

On your storyboard your tabBarController is connected to N UINavigationController S and one of your UINavigationController to your MapViewController. 在情节提要板上,您的tabBarController连接到N个 UINavigationController S,并且其中一个UINavigationController连接到MapViewController。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM