使用 Storyboard 时实例化 ViewController

Instantiate ViewController when using Storyboard

我正在使用 Storyboard 并希望在单击选项卡栏项目时推送视图控制器以在 UITabBarController 中显示。

为此,我使用现有的 ViewController 链接到 Storyboard 上创建的 TabBarController 并使用以下代码。但是,我得到异常,Storyboard 不包含标识符为 '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 和 Xcode 7。请帮忙

试试下面的代码: 让故事板 = UIStoryboard(名称:"Main",包:无) 让 VC = storyboard.instantiateViewControllerWithIdentifier("SwitchViewController")

检查您的故事板 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
}