问题
I want to create the following user flow in my Swift application:
- User clicks a button
- A modal view pops over with a choice. The user selects a choice.
- 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.
- 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?
回答1:
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.
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)
}
来源:https://stackoverflow.com/questions/38681691/how-to-pop-back-to-a-view-in-a-different-navigation-controller