0

I want to present two view controllers in a row, without seeing the first one. I want to be able to return from the second one to the first.

I have read this post, but the responses focus on using the navigation controller, while I want to present the second view controller modally.

The use case is: My initial VC check if the user is logged in, and presents the login VC if not. If yes, it displays the main VC. On logout, I should be able to unwind to the login VC.

Hugal31
  • 1,610
  • 1
  • 14
  • 27

1 Answers1

1

A suitable solution would be presenting the second view controller first and only presenting the first one after the second one fully appeared.

InitialViewController

let secondVc = storyboard!.instantiateViewController(withIdentifier: "Second") as! SecondViewController
secondVc.initialVc = self
present(secondVc, animated: true)

SecondViewController

fileprivate var firstViewDidAppearTime = true
var initialVc: InitialViewController!

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    if firstViewDidAppearTime {
        firstViewDidAppearTime = false

        let firstVc = storyboard!.instantiateViewController(withIdentifier: "First")
        initialVc.present(firstVc, animated: false)
    }
}
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223