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

标签 ios xcode swift swift2

我目前正在尝试将我的 Swift iOS 应用程序转换为 Swift 2。我已经设法消除了编译器错误(在 stackoverflow 的帮助下),但是一旦应用程序现在编译,我就会遇到问题。我的应用程序有一个带有 3 个选项卡的 tabBarController。当我选择第二个选项卡时,应用程序现在崩溃并出现“SIGABRT”错误。

我得到错误:

Could not cast value of type 'UINavigationController' (0x106fbd588) to 'FlightTracker.MapViewController' (0x105722430)

对于下面的代码:

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

为什么它不能将 Controller 转换为 MapViewController?在 iOS 9 和 Swift 2 之前,我以这种方式工作。

最佳答案

它应该工作

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] => 这是你的 UINavigationController

在您的 Storyboard上,您的 tabBarController 连接到 N UINavigationControllerS 并且您的 UINavigationController 之一连接到您的 MapViewController。

关于ios - Swift - 无法将类型 'UINavigationController' 的值转换为 'MapViewController',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34301221/

相关文章:

ios - swift Eureka : Can't dynamically hide/show ButtonRow inside cellUpdate

android - 如何为 Apple app store 或 Google play 制作一个 url 以实际打开 app store?

ios - 无法安装 Codename 一个 iOS 调试应用程序

ios - 自定义 Unwind Segue 在 Swift 中因 NSException 而崩溃

ios - OCMock 设置模拟类,其属性并获得基本了解

ios - UIPageViewController 具有以编程方式生成的 View 数组

c++ - 在/usr/bin 中删除并复制回来后出现 Clang 错误

ios - Xcode 9 - iOS 8 图像奇怪地扭曲

ios - UITableViewController 不使用 xib 文件加载自定义单元格

swift - 相等是否意味着哈希值相等?