2017-06-20 78 views
0

我試圖推動新的視圖控制器,但死機的第一行與此錯誤消息爲什麼在實例化視圖控制器時應用程序崩潰?

EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

我有雙三重檢查類的名稱和故事板ID和他們是正確的。不知道問題是什麼。

func showUsers() { 
    let vc = self.storyboard?.instantiateViewController(withIdentifier: "group") as! GroupViewController 
    self.navigationController?.pushViewController(vc, animated: true) 
} 
+0

任何消息? – Larme

+0

@Larme在控制檯沒有消息。您的故事板中的 – joethemow

+0

Groupviewcontroller是否按組識別? –

回答

3

你能檢查你的vc是不是零?

您有可選的storyboard和強制as,在推vc之前,有幾件事可能會出錯。它可以是nil,也可以不是GroupViewController實例。

你可以通過嘗試這樣的東西開始:

if let vc = self.storyboard?.instantiateViewController(withIdentifier: "group") as? GroupViewController { 
    self.navigationController?.pushViewController(vc, animated: true) 
} 

,看你是否真正得到非空vc

編輯:根據你的評論 - 看起來你確實得到nil在某些時候。然後你需要檢查確切的地方。你的self.storyboard是不是零?

你可以實例化你的視圖控制器嗎? method docs

If the specified identifier does not exist (or is nil) in the storyboard file, this method raises an exception.

我建議打破你實例爲單獨的步驟,並檢查他們每個人這樣的調試:在控制檯

if let storyboard = self.storyboard { 
    let vc = storyboard.instantiateViewController(withIdentifier: "group") 
    if let groupVC = vc as? GroupViewController { 
     self.navigationController?.pushViewController(groupVC, animated: true) 
    } 
} 
+0

是的,如果故事板= self.storyboard',它就會失敗。現在正常工作:) – joethemow

0

請檢查下面給定的點: -

1)請檢查用於GroupViewController駐留在相同的故事板或不

2)調試代碼,並檢查對於VC對象它不應該是零。

3)請檢查羣組 GroupViewController的標識符,特別是故事板。

相關問題