2016-11-29 97 views
0

我创建了一个名称为SomeViewController的NIB,并且所有相应的代码都是正确的,并且所有视图都被正确绑定,但代码self.presentViewController(SomeViewController(), animated: true, completion: nil)导致crash:呈现一个NIB在iOS 8上崩溃但不在iOS 9+

fatal error: unexpectedly found nil while unwrapping an Optional value

问题是什么?

+0

什么是崩溃日志固定的吗? –

+0

@AhmadF'致命错误:意外地发现零,同时展开一个可选值'我已经修复它,但我的答案在下面。这很奇怪,为什么会发生这种情况 –

回答

2

为了做这个

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) 
+1

你应该标记你的答案,因为现在这看起来像任何有效的解决方案:)。 –

+0

我不得不等待2天来标记我自己的答案哈哈 –