问题
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?
回答1:
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 including the nibName on initialization with it's corresponding class.
Update:
Can also be fixed by doing this
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)
来源:https://stackoverflow.com/questions/40860839/presenting-a-nib-modally-crashes-on-ios-8-but-not-on-ios-9