![](/img/trans.png)
[英]Instantiate ViewController with identifier causes a crash using storyboard and a xib
[英]Instantiate ViewController when using Storyboard
我正在使用Storyboard
並希望在單擊選項卡欄項目時將其顯示在UITabBarController
中的視圖控制器。
為此,我使用鏈接到在Storyboard
創建的TabBarController
的現有ViewController
並使用以下代碼。 但是,我得到了例外, 情節提要不包含帶有標識符“ SwitchViewController”的視圖控制器
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
let switchViewController = self.storyboard?.instantiateViewControllerWithIdentifier("SwitchViewController")
self.presentViewController(switchViewController, animated: true, completion: nil)
return false
}
更新 :我在Main.storyboard
為SwitchViewController
添加了Storyboard ID
,並注釋了上面的代碼。 我移至此標簽時,應用失敗,出現相同錯誤
我正在使用iOS 8和Xcode7。請幫助
檢查您的情節提要ID,
試試這個
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
if viewController is SwitchViewController {
let switchViewController = self.storyboard?.instantiateViewControllerWithIdentifier("SwitchViewController") as! SwitchViewController
self.presentViewController(switchViewController, animated: true, completion: nil)
return false
}
return true
}
嘗試以下代碼:let storyboard = UIStoryboard(name:“ Main”,bundle:nil)let VC = storyboard.instantiateViewControllerWithIdentifier(“ SwitchViewController”)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.