swiftuiviewcontrollerpushviewcontrollerpopviewcontroller

Pop old viewController


So I have 3 view controllers (and a nav view controller). Starting at VC1 I push to VC2 then when the scene for VC2 is done I pop VC2 then push to VC3. Only problem with that is that the user can see the stack push/popping. Is there anyway I can push VC3 then pop VC2 without it being noted? I know it's a stack so you can't necessarily pick elements as you can arrays, but maybe there is a way I can pop VC2 from VC3?

I saw a few stackoverflow resources but all in old syntax from 2011-2014 and are very outdated. Anyone have any ideas?


Solution

  • Yes you can. You can use the setViewControllers:animated: method. Like this:

    navigationController?.setViewControllers([vc1, vc3], animated: true)
    

    This will animate a push while removing the vc2 from the stack and replacing it with vc3. More info about the method here.

    Update:

    If you don't have a reference to vc1 in v2 (and you probably don't) you can do this:

    navigationController?.setViewControllers(navigationController!.viewControllers.first!, vc3], animated: true)
    

    Oh, and if you wish to push vc3 without any animation then, obviously, just call the method with animated: false.