2017-10-08 67 views
-1

當我試圖在XCode 9上使用Swift 4從一個視圖切換到另一個視圖時,它給了我這個錯誤。似乎無法通過UIButton切換到另一個故事板

的libC++ abi.dylib:與 類型NSException

的未捕獲的異常終止我使用一個按鈕,切換從MainLandingViewMain是默認視圖(main.storyboard),而LandingViewmain.storyboard中的另一個視圖控制器。

下面是我用來使這個工作的代碼。

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
let newViewController = storyBoard.instantiateViewController(withIdentifier: "LandingView") 
self.present(newViewController, animated: true, completion: nil) 

[above]所有包含在我想要使用的按鈕的IBAction中。

只是爲了澄清,LandingView是一個故事板ID,我用它來從默認視圖切換到LandingView。請注意,這全部包含在Main.storyboard之下。

+0

發佈一些您的崩潰日誌的更多信息。如果您使用的是Storyboard,請確保您爲每個屏幕指定了唯一的「標識符」。 – Glenn

+0

改用segues。修復了這個問題。 – Sanjay

回答

0

您還沒有指定newViewController的類型。

試試這個:

let storyBoard: UIStoryBoard = UIStoryBoard(name: "Main", bundle: nil) 
    let newViewController = storyboard.instantiateViewController(withIdentifier: "LandingView") as! younewViewControllerType 
    self.present(newViewController, animated: true, completion: nil) 

要確定你已經宣佈你LandingView的ViewController子類,你可以在界面生成圖像中引用的身份檢查如下:

enter image description here

在自定義Class,Class屬性定義了你的自定義子類。在聲明你的newViewController時,你必須聲明它爲那種類型。

+0

這個類型是什麼? – Sanjay

+0

這可能是LandingViewController。 –

+0

使用未聲明的類型「UIStoryBoard」錯誤。你確定這是Swift 4嗎? – Sanjay