我有三个视图控制器嵌入导航控制器.我想从VC1到VC3,以便在VC3中,导航项的后退按钮可以将用户定向到VC2而不是VC1.我认为这应该是通过在VC3创建之后将VC2添加到VC1和VC3之间的导航堆栈或通过跳过第二个View Controller来完成的.
我试过这个:
override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) { if let identifier = segue.identifier { switch identifier { case "JumpToThirdVCSegue": if let secondvc = segue.destinationViewController as? SecondViewController { secondvc.performSegueWithIdentifier("ThirdVCSegue",sender: self) } default: break } } }
但我不能让它工作.当View Controller尚未打开时,是否可能执行segue?
解决方法
这样的事情应该有效:
self.navigationController?.pushViewController(viewController2,animated: true) self.navigationController?.pushViewController(viewController3,animated: true)
编辑:
如果您希望推送第二个视图控制器,而不会被用户注意到,则在按下第三个视图控制器之后,您需要将其添加到导航控制器.这可以通过实现UINavigationControllerDelegate来实现.您可以将第二个视图控制器存储在变量中,并使用委托方法将其插入到导航控制器层次结构中.您的主视图控制器将如下所示:
class MyViewController: UIViewController,UINavigationControllerDelegate { var viewControllerToInsertBelow : UIViewController? override func viewDidLoad() { super.viewDidLoad() self.navigationController?.delegate = self } func pushTwoViewControllers() { if let viewController2 = self.storyboard?.instantiateViewControllerWithIdentifier("id1"),let viewController3 = self.storyboard?.instantiateViewControllerWithIdentifier("id2") { //change this to your identifiers self.viewControllerToInsertBelow = viewController2 self.navigationController?.pushViewController(viewController3,animated: true) } } //MARK: - UINavigationControllerDelegate func navigationController(navigationController: UINavigationController,didShowViewController viewController: UIViewController,animated: Bool) { if let vc = viewControllerToInsertBelow { viewControllerToInsertBelow = nil let index = navigationController.viewControllers.indexOf(viewController)! navigationController.viewControllers.insert(vc,atIndex: index) } } }