Presenting a NIB Modally Crashes on iOS 8 but not on iOS 9+

烈酒焚心 提交于 2020-01-26 04:15:43

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!