Is it possible to insert a UIViewController between two view controllers of an UINavigationController?

I want to push from VC 1 to VC 3 but when VC 3 pops I want to be on VC 2. Is there a way I can secretly put VC 2 infront of VC 3 before poping VC 3?
Answered by Alipacman in 621381022
I just found the answer:
Code Block
self.navigationController?.viewControllers.insert(MenuVC, at: 1)

after pushing the CanvasVC.
Could you explain your intent ? What do you want to achieve ? What do you want user to see and not see ?

What fo you mean "you want to "secretly (?!?) put VC2 in front of VC3"
  • do you just want to load VC2 ? But no show ?

  • If so, what does "in front VC3" mean ?

How do you navigate to VC3 ? With segue ?
If so, in prepare, you could just load VC2 but not present it.
See:
https://stackoverflow.com/questions/25745854/how-to-initiate-a-viewcontroller-without-presenting-it
Thank you for your reply, this is the detailed case of my drawing app:

I have three view controllers:
  1. IntroVC

  2. MenuVC

  3. CanvasVC

The usual flow is to present the IntroVC on app start:
Code Block
let navigationController = UINavigationController(rootViewController: IntroVC())
self.window?.rootViewController = navigationController

Then after the IntroVC is finished I push to the MenuVC with a custom animation:
Code Block
self.navigationController?.pushViewControllerWithWaveTransition(to: MenuVC())

When the user decides to start drawing the MenuVC will push to the CanvasVC, like this:
Code Block
self.navigationController?.pushViewController(pageVCC, animated: true)

After the user finishes his drawing he can pop to the MenuVC again, like this:
Code Block
self.navigationController?.popViewController(animated: true)


This is the usual flow, but on the first start, instead of presenting the IntroVC a OnboardingVC is presented.
The Onboarding has a little introduction and after the introduction it should push the user into the CanvasVC directly without pushing the MenuVC.
When the user is done in the CanvasVC I want to call:
Code Block
self.navigationController?.popViewController(animated: true)

and be in the MenuVC again, not in the OnboardingVC. There for I asked, if I can put the MenuVC inbetween the OnboardingVC and the CanvasVC, without pushing it on the navigation stack.

I hope my explanation is understandable :)

Accepted Answer
I just found the answer:
Code Block
self.navigationController?.viewControllers.insert(MenuVC, at: 1)

after pushing the CanvasVC.

if I can put the MenuVC inbetween the OnboardingVC and the CanvasVC, without pushing it on the navigation stack.

Why don't you just add the View over OnboardingVC, with addSubview ?

This is the usual flow, but on the first start, instead of presenting the IntroVC a OnboardingVC is presented. 
The Onboarding has a little introduction and after the introduction it should push the user into the CanvasVC directly without pushing the MenuVC.

Instead of pushing the CanvasVC onto the stack from the OnboardingVC, use the navigation controller's setViewControllers:animated: method to pass an array with the MenuVC and CanvasVC. That will replace the OnboardingVC with the MenuVC and when you pop back from CanvasVC you will return to the MenuVC (and it will be the top-most view controller in the navigation stack).

It's not clear if the user can ever navigate back from the MenuVC to your IntroVC. Probably not. So you could do the same thing to remove IntroVC from the stack and release its memory. Although it sounds like you might be using some sort of custom presentation (pushViewControllerWithWaveTransition) so that might not work.
Is it possible to insert a UIViewController between two view controllers of an UINavigationController?
 
 
Q