在单击按钮后使用多个故事板时,为什么会出现窗口层次结构错误



我在这里遵循一种使用多个故事板的方法:http://www.newventuresoftware.com/blog/organizing-xcode-projects-using-multiple-storyboards/

但是我想在用户按下按钮后切换故事板,我得到的错误是:

警告:尝试在 上显示 SWRevealViewController 主视图控制器视图不在窗口层次结构中!

切换情节提要的代码是:

var storyboard = UIStoryboard(name: "Search", bundle: nil)
var controller = storyboard.instantiateViewControllerWithIdentifier("theRealID") as! UIViewController
self.presentViewController(controller, animated: true, completion: nil)

仅供参考,如果我这样做,我可以加载第二个故事板,但是....这不是我想要的时候。我想要按钮按下后。

    override func viewDidAppear(animated: Bool) {
    //super.viewDidAppear(animated)
    var storyboard = UIStoryboard(name: "Search", bundle: nil)
    var controller = storyboard.instantiateViewControllerWithIdentifier("theRealID") as! UIViewController
    self.presentViewController(controller, animated: true, completion: nil)
}

您只需要为按钮创建一个 IBAction 并实例化一个新的视图控制器,如下所示:

presentViewController( UIStoryboard(name: "Search", bundle: nil).instantiateViewControllerWithIdentifier("theRealID") as! UIViewController, animated: true, completion: nil)

如果需要,还可以将其与导航控制器一起使用:

presentViewController( UIStoryboard(name: "Search", bundle: nil).instantiateViewControllerWithIdentifier("theRealID") as! UINavigationController, animated: true, completion: nil)

最新更新