pyriku pyriku - 1 month ago 6
iOS Question

How can I skip a ViewController when dismissing from navigationController?

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?

ohr ohr
Answer

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
                        }
                    }
        }
Comments