I am creating a simple app with three ViewController in interface builder. Right now they are presented in this order:

FirstVC > SecondVC > ThirdVC

  1. FirstVC

Shows the main menu, a play button is connected via interface builder to show the SecondVC (no code right now).

  1. SecondVC

A simple game. When the game is over I present the ThirdVC programatically.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "GameOverScreen") as UIViewController
    vc.modalPresentationStyle = UIModalPresentationStyle.currentContext
    vc.modalTransitionStyle = .crossDissolve

    self.present(vc, animated: true, completion: nil)

Before (or while) the ThirdVC is presented I want to dismiss the SecondVC so that in the hierarchy only:

FirstVC > ThirdVC

is present.

  1. ThirdVC

Shows a simple game over message.

dismiss(animated: true, completion: nil)

When I dismiss this view controller I want to go back to FirstVC or rather because SecondVC is already dismissed the player is automatically back on FirstVC.

How can I do this? Of course I know how to present and dismiss a VC programmatically, but how can I dismiss a VC at the same time presenting a new VC? Or what would be the right way to do this?

1

There are 1 best solutions below

0
On

You better use navigationcontroller and when ThirdVC is pushed do this in it's viewDidLoad

 func optimizeNavigation()
{


    let viewControllers: [UIViewController] = self.navigationController!.viewControllers ;

    print("before optimize \(viewControllers.count)")

    for aViewController in viewControllers
    {
        if(aViewController is SecondVC)
        {
           aViewController.removeFromParentViewController()
        }
    }

      let viewControllers2: [UIViewController] = self.navigationController!.viewControllers ;

      print("after optimize \(viewControllers2.count)")

}

This way when you pop in ThirdVC you will return to FirstVC