Reputation: 21
I need to return to my rootViewController when the app move from background to foreground. So in applicationWillEnterForeground I have written this code:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainController = storyboard.instantiateViewController(withIdentifier: "MqttLoginController")
window?.rootViewController = mainController
But I don't understand why in my rootViewController is called firstly viewDidAppear, then viewDidLoad and finally viewDidAppear again. Why is this happening?
UPDATE: For clarification, I haven't written that I don't use navigation controller in the initial screens of the app, and I need to come back to my initialViewController. So it's not really the rootViewController of all screens.
Upvotes: 1
Views: 286
Reputation: 14397
Instead of creating a new instance try to use this code
if let root = window?.rootViewController {
root.navigationController?.popToRootViewController(animated: true)
}
Upvotes: 0
Reputation: 16341
The ViewController is already present as the root. Hence, when your app comes to foreground it fires viewDidAppear
then you are creating a new instance of ViewController, this fires the viewDidLoad
and when it appears viewDidAppear
once again.
Upvotes: 1