Swift 在iOS 8上以模式呈现NIB会崩溃,但在iOS 9上不会+;
我创建了一个名为Swift 在iOS 8上以模式呈现NIB会崩溃,但在iOS 9上不会+;,swift,xcode,crash,nib,Swift,Xcode,Crash,Nib,我创建了一个名为SomeViewController的NIB,所有对应的代码都正确,所有视图都绑定正确,但代码self.presentViewController(SomeViewController(),动画:true,完成:nil)会导致崩溃: 致命错误:在展开可选值时意外发现nil 问题是什么?要解决此问题,我们需要通过执行此操作进行版本检查 if #available(iOS 8, *) { self.presentViewController(SomeViewC
SomeViewController
的NIB,所有对应的代码都正确,所有视图都绑定正确,但代码self.presentViewController(SomeViewController(),动画:true,完成:nil)
会导致崩溃:
致命错误:在展开可选值时意外发现nil
问题是什么?要解决此问题,我们需要通过执行此操作进行版本检查
if #available(iOS 8, *) {
self.presentViewController(SomeViewController(nibName: "SomeViewController", bundle: nil), animated: true, completion: nil)
} else {
self.presentViewController(SomeViewController(), animated: true, completion: nil)
}
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)
或者只是
self.presentViewController(SomeViewController(nibName: "SomeViewController", bundle: nil), animated: true, completion: nil)
由于某些原因,iOS 8在初始化时没有自动将nibName包含在其相应的类中
更新:
也可以通过这样做来修复
if #available(iOS 8, *) {
self.presentViewController(SomeViewController(nibName: "SomeViewController", bundle: nil), animated: true, completion: nil)
} else {
self.presentViewController(SomeViewController(), animated: true, completion: nil)
}
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)
要解决这个问题,我们需要通过执行以下操作进行版本检查
if #available(iOS 8, *) {
self.presentViewController(SomeViewController(nibName: "SomeViewController", bundle: nil), animated: true, completion: nil)
} else {
self.presentViewController(SomeViewController(), animated: true, completion: nil)
}
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)
或者只是
self.presentViewController(SomeViewController(nibName: "SomeViewController", bundle: nil), animated: true, completion: nil)
由于某些原因,iOS 8在初始化时没有自动将nibName包含在其相应的类中
更新:
也可以通过这样做来修复
if #available(iOS 8, *) {
self.presentViewController(SomeViewController(nibName: "SomeViewController", bundle: nil), animated: true, completion: nil)
} else {
self.presentViewController(SomeViewController(), animated: true, completion: nil)
}
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)
崩溃日志是什么?@AhmadF
致命错误:在打开可选值时意外发现nil
我已经用下面的答案修复了它。尽管崩溃日志是什么,但为什么会发生这种情况还是很奇怪的?@AhmadF致命错误:在打开可选值时意外地发现了nil
我已经用下面的答案修复了它。虽然你应该标记你的答案,但为什么会发生这种情况却很奇怪,因为现在这似乎和任何答案一样有效:)。我不得不等2天来标记我自己的答案。哈哈,你应该标记你的答案,因为现在这似乎和任何答案一样有效:)。我不得不等2天来标记我自己的答案哈哈