我创建了一个名为 SomeViewController
的 NIB,所有相应的代码都是正确的,所有的 View 都正确绑定(bind),但不知何故代码 self.presentViewController(SomeViewController() , animated: true, completion: nil)
导致崩溃:
fatal error: unexpectedly found nil while unwrapping an Optional value
问题是什么?
最佳答案
为了解决这个问题,我们需要通过这样做来检查版本
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)
关于swift - 在 iOS 8 上呈现 NIB 模态崩溃,但在 iOS 9+ 上不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40860839/