Jan
Jan

Reputation: 2522

Push view controller, but go back to another one

Let's say that on my regular flow I have VC1 -> VC2 -> VC3 -> VC4

But I have a requirement that when I touch a special button on VC1 it needs to go to VC4.

No problem there. The problem is that when I tap the back button on VC4, it needs to go back to VC3 instead of VC1.

I already tried pushing From VC1 -> VC2 and VC2 -> VC3 without animation and then VC3 -> VC4 with animation, but you can see a quick glimpse of VC3 which looks awful.

Any ideas?

Upvotes: 1

Views: 1135

Answers (2)

janusfidel
janusfidel

Reputation: 8106

You can modify the UINavCon's viewControllers to achieve the order you want.

func specialButtonInVc1() {
    self.navigationController?.pushViewController(fourth, animated: true)
    self.navigationController?.viewControllers = [self, second, third,fourth]
}

Upvotes: 2

Derek
Derek

Reputation: 1021

I think in this specific case, you can insert a view controller on the navigation controllers stack after presenting the 4th viewController

if let navigationController = navigationController {
  navigationController.pushViewController(vc4, animated: true)

  let vc3Index = navigationController.viewControllers.count - 1
  navigationController.viewControllers.insert(vc3, atIndex: vc3Index)
}

This should place VC3 next in line when the user presses back from VC4. Untested code, btw.

Upvotes: 4

Related Questions