How to pop back to a view in a different navigation controller?

泪湿孤枕 提交于 2019-12-07 18:43:59

问题


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?


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!