2016-11-24 43 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") 
    } 

編寫安全的代碼如上,並確保你已經添加在故事板的標識符和正確分配的類別的視圖控制器將返回零。