【发布时间】:2016-08-19 11:57:33
【问题描述】:
我有一个ContainerVC,其中包含一个变量controllerToLoad。默认情况下它是“MainVC”,但是当单击菜单项时,我希望显示ContainerVC,而不是加载MainVC,我希望加载SecondVC。
我通过执行以下操作在 ContainerVC 的 ViewDidLoad 方法中加载 MainVC:
if let controller = self.storyboard?.instantiateViewControllerWithIdentifier(controllerToLoad) {
self.mainViewController = controller
}
我认为最好的方法是通过 segue,所以我调用以下代码行:
self.performSegueWithIdentifier("MenuSegue", sender: self)
然后在我的prepareForSegue 中执行以下操作:
if (segue.identifier == "MenuSegue"){
let secondViewController = segue.destinationViewController as! ContainerViewController
secondViewController.controllerToLoad = "SecondVC"
}
由于某种原因,当调用performSegueWithIdentifier 时,它会自动加载ContainerVC 并开始将self.mainViewController 设置为“MainVC”,而不是等待prepareForSegue 运行。我不知道为什么它不等待prepareForSegue 完成。
我通过添加一堆打印语句对此进行了测试。就在self.performSegueWithIdentifier 之前和secondViewController.controllerToLoad = "SecondVC" 之前以及我的ContainerVC 的ViewDidLoad 之前。添加打印语句后,我意识到实际上只要调用performSegueWithIdentifier,就在它调用ViewDidLoad 的ContainerVC 然后执行
let secondViewController = segue.destinationViewController as! ContainerViewController
secondViewController.controllerToLoad = "SecondVC"
【问题讨论】:
标签: ios swift model-view-controller uiviewcontroller segue