0

Say that you have some ViewControllers like:

A --> B --> C --> X --> D

Where X is some sort of perpetual menu that can be opened from everywhere. All of those are plugged to a NavigationController, so for going back I would do something like:

func leftNavButtonClick(sender: UIButton) { navigationController?.popViewControllerAnimated(true) }

However if I do that from D, I want to go back to C, not X. I tried dismissing the menu before opening the next ViewController but that doesn't seem to work very well.

Any ideas?

pyriku
  • 1,251
  • 7
  • 17
  • 1
    This will help you: http://stackoverflow.com/questions/34714845/how-to-create-a-back-button-in-a-view-controller-to-go-to-parent-view-controller/34714903#34714903 – Bista Oct 17 '16 at 16:16

1 Answers1

2

You can use the view controller stack to find your VC and pop to it

if let vcStack = self.navigationController?.viewControllers
        {
            for vc in vcStack {
                        if vc.isKindOfClass(MyVC)
                        {                           
                            self.navigationController?.popToViewController(vc, animated: true)
                            break
                        }
                    }
        }
ohr
  • 1,717
  • 14
  • 15