在iOS 8上显示NIB模式崩溃,但在iOS 9+上不显示 [英] Presenting a NIB Modally Crashes on iOS 8 but not on iOS 9+
问题描述
我创建了一个名为 SomeViewController
的NIB,所有相应的代码都是正确的,所有的视图都绑定正确,但不知何故代码 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)
}
或者只是
self.presentViewController(someViewController(nibName:SomeViewController,bundle:nil),animated:true,completion:nil )
因为某些原因iOS 8不能自动标记nibName与其对应的类
I have created a NIB which has a name of SomeViewController
and all the corresponding code are correct and all the views are bound correctly, but somehow the code self.presentViewController(SomeViewController(), animated: true, completion: nil)
causes a crash:
fatal error: unexpectedly found nil while unwrapping an Optional value
What is the problem?
To fix this we need to version check by doing this
if #available(iOS 8, *) {
self.presentViewController(SomeViewController(nibName: "SomeViewController", bundle: nil), animated: true, completion: nil)
} else {
self.presentViewController(SomeViewController(), animated: true, completion: nil)
}
or just
self.presentViewController(SomeViewController(nibName: "SomeViewController", bundle: nil), animated: true, completion: nil)
for some reason iOS 8's not automated on tagging the nibName with it's corresponding class
这篇关于在iOS 8上显示NIB模式崩溃,但在iOS 9+上不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!