I am creating a simple app with three ViewController in interface builder. Right now they are presented in this order:
FirstVC > SecondVC > ThirdVC
- FirstVC
Shows the main menu, a play button is connected via interface builder to show the SecondVC (no code right now).
- 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.
- 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?
You better use navigationcontroller and when ThirdVC is pushed do this in it's viewDidLoad
This way when you pop in ThirdVC you will return to FirstVC