Skip/Add a View Controller in Navigation Stack
Something like this should work:
self.navigationController?.pushViewController(viewController2, animated: true) self.navigationController?.pushViewController(viewController3, animated: true)
EDIT:
If you want to push the second view controller without being noticed by the user you need to add it to the navigation controller after the third view controller is pushed. This can be done by implementing UINavigationControllerDelegate
. You can store your second view controller in a variable and insert it to the navigation controllers hierarchy in delegate method. Your main view controller will look like following:
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) } }}
if var navstack = navigationController?.viewControllers{ navstack.append(contentsOf: [vc1,vc2]) navigationController?.setViewControllers(navstack, animated: true) }
Works well
Edit: using swift and segues, this should work:
override func performSegueWithIdentifier(identifier: String?, sender: AnyObject?) { super.performSegueWithIdentifier(identifier, sender: sender); if identifier == "JumpToThirdVCSegue" { // now the vc3 was already pushed on the navigationStack var navStackArray : [AnyObject]! = self.navigationController!.viewControllers // insert vc2 at second last position navStackArray.insert(viewController2, atIndex: navStackArray.count - 2) // update navigationController viewControllers self.navigationController!.setViewControllers(navStackArray, animated:false) }}
So you override the performSegueWithIdentifier in the VC1 in order to change the navigation stack when the segue to VC3 has actually been performed (and is not only in preparation).This assumes, that you want to handle this special navigation logic in VC1.