簡體   English   中英

使用Storyboard時實例化ViewController

[英]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.storyboardSwitchViewController添加了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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM