2015-05-14 33 views
1

此選擇一個視圖是代碼:的XCode 6:我無法通過其故事板ID

let theBoard = self.storyboard! 
let vc = theBoard.instantiateViewControllerWithIdentifier("myViewId") as! UIViewController // this line causes the error 

第二行導致此錯誤:

unexpectedly found nil while unwrapping an Optional value 

*非常重要*:該錯誤僅在構建配置設置爲「debug」時觸發。當設置爲「release」時,它運行良好。

爲什麼?

+0

你確定當你釋放它顯示你的目的地n故事板? –

+0

絕對,在發佈視圖顯示 – Cherif

+0

清理項目和重建 –

回答

0

您正在將UIViewController投射到let vc = theBoard.instantiateViewControllerWithIdentifier("myViewId") as! UIViewController這是錯誤的。 你應該嘗試:

var storyboard = UIStoryboard(name: "Main", bundle: nil) 

var vc = storyboard.instantiateViewControllerWithIdentifier("myViewId") as! ViewController 
0

使用這樣

var moveToNextVC:ViewController = self.storyboard?.instantiateViewControllerWithIdentifier("myViewId") as! ViewController 

self.presentViewController(moveToNextVC, animated: true, completion: nil) 

記住設置StoryboardID確定「myViewId」的文件檢查您的視圖 - 控制

0

這一切是新的語法,功能問題並沒有改變:

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil) 
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController 
self.presentViewController(vc, animated: true, completion: nil) 
+0

我試過這個解決方案,但它爲什麼在版本中工作,而不是在調試? – Cherif

+0

它在調試中也適用於我...... –

+0

可能有些配置存在某些錯誤。這是我正在尋找的。 – Cherif