2016-11-24 44 views
-3

我试图打开新的观点instantiateViewController:试图打开新的观点

ViewController.swift - > InAppBrowserController.swift

Main.storyboard - > InAppBrowser查看使&故事板标识 - >确定。

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let nextViewController = storyboard.instantiateViewController(withIdentifier: "InAppBrowserView") as! InAppBrowserController 
self.present(nextViewController, animated: true, completion: nil) 

Blockquote fatal error: unexpectedly found nil while unwrapping an Optional value 2016-11-24 20:24:50.569000 GMK SWork[5920:2148780] fatal error: unexpectedly found nil while unwrapping an Optional value

问题是什么?

回答

0

在你的故事板,你必须设置标识符的UIViewController,然后才能以这种方式获得:

let yourController = UIStoryboard.init(name: @"yourStoryboardName", bundle: nil).instantiateViewController(withIdentifier: "yourVCIdentifier") as? UIViewController 

重要的是要设置标识符(我认为你的控制器找不到此)

enter image description here

在任何情况下

如果你没有特殊的需求,你应该从故事板使用直接“SEGUE”,推动/带出示您的控制器:

self.performSegue(withIdentifier: "yourVCIdentifier", sender: self) 
+1

我已经试过它 但它帮了我很多。 谢谢!〜 –

0
let storyBoard = UIStoryboard(name: "Main", bundle: nil) 
if let controller = storyBoard.instantiateViewController(withIdentifier: "InAppBrowserView") as? InAppBrowserController { 
    self.present(controller, animated: true, completion: nil) 
    } else { 
     print("Controller not found") 
    } 

编写安全的代码如上,并确保你已经添加在故事板的标识符和正确分配的类别的视图控制器将返回零。