3

I want to create the following user flow in my Swift application:

  1. User clicks a button
  2. A modal view pops over with a choice. The user selects a choice.
  3. A second view appears with another choice (using a "show" transition, so the user can press < Back to alter choice #1). The user selects the second choice.
  4. The second modal view segues back to the original view (which has the button).

To do this, I created the following structure:

--> NC1 --> VC1 --modal-> NC2 --> VC2 --show-> VC3    (NC = UINavigationController,
             ^                                  |      VC = UIViewController)
             |                                  |
             ----Segue I'm trying to achieve-----

I've tried to pop all the way back to VC1 by passing a reference to VC1 all the way through to VC3, then calling the following code in VC3:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    // Perform Segue
    print("Trying to segue")
    rootController!.navigationController!.popToRootViewControllerAnimated(true)

    tableView.deselectRowAtIndexPath(indexPath, animated: true)
} 

where rootController is the reference to VC1. Unfortunately the segue doesn't occur; the second modal view stays on the screen, but I get the "Trying to segue" message printed.

Am I trying to implement this user flow the right way? If so, any suggestions why I can't pop to a root view in another navigation controller?

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • in your navigation controller there is a property called viewControllers that contain all the controllers in you have navigated, remove those that you want to skip – Mohsen Shakiba Jul 31 '16 at 07:25

1 Answers1

4

If you are using storyboards, you can do this using an unwind segue.

In VC1 implement an @IBAction method, which takes a UIStoryboardSegue as parameter:

@IBAction func unwindToVC1(sender: UIStoryboardSegue) {

}

Then from the Prototype cell of VC3 control-drag to the Exit icon of the view controller, select Selection Segue: unwindToVC1: and you are good to go.

enter image description here

You can achieve a similar result using code also.
Implement didSelectRowAtIndexPath in VC3 like this:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    navigationController!.dismissViewControllerAnimated(true, completion: nil)
}
Viktor Simkó
  • 2,607
  • 16
  • 22