ios - 解包 View Controller 值时出现 fatal error

标签 ios swift

我在 Storyboard中有一些具有某些标识符的 View Controller ,当我尝试像这样访问它们时:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let mainViewController = sideMenuController!
    let vcName = identities[indexPath.row]
    let viewController: UIViewController! = storyboard?.instantiateViewController(withIdentifier: vcName)
    let navigationController = mainViewController.rootViewController as! NavigationController
    navigationController.pushViewController(viewController, animated: true)
    mainViewController.hideLeftView(animated: true, completionHandler: nil)

}

应用程序崩溃。该行检测到该值为 nil。谁能解释一下在展开时这是如何为零的?

navigationController.pushViewController(viewController, animated: true)

经过几次尝试,我设法摆脱了 fatal error 。但是,点击表格 View 项后, View Controller 仍然不会出现。

  • 原始项目 - link

  • 已编辑的项目 - link

最佳答案

嗯,这个项目有点“困惑”:-)

首先:您需要以某种方式获取“UINavigationController”(或派生类)。

由于您的LeftViewController不在导航堆栈上,因此它没有设置navigationController。因此,您需要从 mainViewController 获取它:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    print(storyboard)
    let mainViewController = sideMenuController!
    let vcName = identities[indexPath.row]
    let viewController: UIViewController! = storyboard.instantiateViewController(withIdentifier: vcName)
    print(viewController)
    if let theNavigationController = mainViewController.rootViewController as? UINavigationController {
        print("Gotcha")
        theNavigationController.pushViewController(viewController!, animated: true)
        mainViewController.hideLeftView(animated: true, completionHandler: nil)
    }

}

第二,还有一个问题:

标识符为“A”的 View Controller 的类型为EngineTunningParamteres,派生自UINavigationController。您无法将导航 Controller 推送到现有导航 Controller 上。因此,我们也必须更改类:

class EngineTunningParamteres: UIViewController {

    override func viewDidLoad() {
        print("hi")    }

}

现在可以了。不客气。

关于ios - 解包 View Controller 值时出现 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41874302/

相关文章:

swift - UILabel 居中文本 CGRect?

ios - 除了关闭 Bitcode 之外,您如何找出优化 IPA 编译大小的方法?

arrays - 如何使用条件过滤自定义对象数组

swift - 'InfoKey' 不是 'UIImagePickerController' 的成员类型

ios - xcode swift Storyboard引用不起作用

swift - UISegmentedControl 通过代码自定义

iphone - UITableView 在选定行下方添加行

c# - 是否可以在 MonoTouch 中对 UILabel 的文本使用 getter/setter?

ios - uitableview] 如何防止用户在数据完全加载之前滚动?

iOS, swift : play background music and sound effects without delay