2016-09-24 57 views
1

我想要實現的背景:展開segue干擾與展示segue

MainViewController呈現TableVC。 我將點擊一個單元格以顯示DetailVC。 能夠在TableVC或DetailVC上的導航欄上關閉doneBarButton。

所有doneBarButton都植根於MainViewController,所以我會認爲無論視圖控制器會忽略MainViewController。

我遇到的是敲擊我的TableVC的doneBarButton,但不在我的DetailVC的doneBarButton上時發生崩潰。因爲他們都是植根於通過界面生成器的MainViewController我不認爲我應該有代碼的TableVC的prepareForSegue方法什麼但是開關的情況下使標識符被聚焦和調試控制檯輸出

"fatal error: unexpectedly found nil while unwrapping an Optional value"

我m並不完全知道在哪裏查明需要修復的東西,無論是在界面構建器中還是在prepareForSegue方法中包含某些東西。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
{ 
    switch segue.identifier! // Line highlighted with error in debug console 
    { 
    case "toDetailVC": // Properly presents detailVC through a cell being tapped 

    default: 
    break 
    } 
} 

回答

1

你還沒有指定一個標識符開卷SEGUE,所以屬性是nil。由於您用!強制解包,因此崩潰。

要解決該問題,發現在文檔大綱鑑於開卷SEGUE。選擇它,然後在Attributes Inspector中,給segue一個標識符。

Xcode interface showing unwind segue ID


替代的解決方案:

因爲你很可能不關心開卷標識,您也可以通過選擇一個更安全的方式解決問題,以解開了segue.identifier

switch segue.identifier ?? "" { 

這種使用零合併o perator??用空字符串替換未分配的標識符。

+0

哇,這超級簡單,我真的很喜歡使用零合併操作符!沒想到我需要標識符,所以我爲什麼離開它,很高興操作員保持代碼乾淨簡潔! – lifewithelliott