I have a small iPhone app, which uses a navigation controller to display 3 views (here fullscreen):
the swift 2 version of ima747 answer:
override func perform() {
let navigationController: UINavigationController = sourceViewController.navigationController!;
var controllerStack = navigationController.viewControllers;
let index = controllerStack.indexOf(sourceViewController);
controllerStack[index!] = destinationViewController
navigationController.setViewControllers(controllerStack, animated: true);
}
As he mentioned it has the following advantages:
Using unwind segue would be the most appropriate solution to this problem. I agree with Lauro.
Here is a brief explanation to setup an unwind segue from detailsViewController[or viewController3] to myAuthViewController[or viewController1]
This is essentially how you would go about performing an unwind segue through the code.
Implement an IBAction method in the viewController you want to unwind to(in this case viewController1). The method name can be anything so long that it takes one argument of the type UIStoryboardSegue.
@IBAction func unwindToMyAuth(segue: UIStoryboardSegue) {
println("segue with ID: %@", segue.Identifier)
}
Link this method in the viewController(3) you want to unwind from. To link, right click(double finger tap) on the exit icon at the top of the viewController, at this point 'unwindToMyAuth' method will show in the pop up box. Control click from this method to the first icon, the viewController icon(also present at the top of the viewController, in the same row as the exit icon). Select the 'manual' option that pops up.
In the Document outline, for the same view(viewController3), select the unwind segue you just created. Go to the attributed inspector and assign a unique identifier for this unwind segue. We now have a generic unwind segue ready to be used.
Now, the unwind segue can be performed just like any other segue from the code.
performSegueWithIdentifier("unwind.to.myauth", sender: nil)
This approach, will take you from viewController3 to viewController1 without the need to remove viewController2 from the navigation hierarchy.
Unlike other segues, unwind segues do not instantiate a view controller, they only go to an existing view controller in the navigation hierarchy.