【问题标题】:Presenting a NIB Modally Crashes on iOS 8 but not on iOS 9+在 iOS 8 上呈现 NIB 模态崩溃,但在 iOS 9+ 上不呈现
【发布时间】:2016-11-29 08:17:13
【问题描述】:

我创建了一个名称为SomeViewController 的NIB,并且所有相应的代码都是正确的,并且所有视图都正确绑定,但不知何故代码self.presentViewController(SomeViewController(), animated: true, completion: nil) 会导致崩溃:

致命错误:在展开可选值时意外发现 nil

有什么问题?

【问题讨论】:

  • 什么是崩溃日志?
  • @AhmadF fatal error: unexpectedly found nil while unwrapping an Optional value 我已经用下面的答案修复了它。但是为什么会发生这种情况很奇怪

标签: swift xcode crash nib


【解决方案1】:

要解决这个问题,我们需要通过这样做来检查版本

    if #available(iOS 8, *) {
        self.presentViewController(SomeViewController(nibName: "SomeViewController", bundle: nil), animated: true, completion: nil)
    } else {
        self.presentViewController(SomeViewController(), animated: true, completion: nil)
    }

或者只是

self.presentViewController(SomeViewController(nibName: "SomeViewController", bundle: nil), animated: true, completion: nil)

由于某种原因,iOS 8 没有自动在初始化时将 nibName 包含在其对应的类中。

更新:

也可以这样解决

class SomeViewController: UIViewController {
    init() {
        super.init(nibName: "SomeViewController'sNibNameHere", bundle: nil)
    }
}

// on some other part of your code
self.presentViewController(SomeViewController(), animated: true, completion: nil)

【讨论】:

  • 你应该标记你的答案,因为现在这似乎是任何有效的解决方案:)。
  • 我不得不等待 2 天来标记自己的答案哈哈
猜你喜欢
  • 2012-09-13
  • 1970-01-01
  • 2018-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多