Search code examples
iosswiftuiviewcontrollerviewdidloaddismiss

Is it possible to prepare/reset a scene before it is shown / before viewDidAppear / viewDidLoad?


I am having a viewController with a couple of animated buttons, when the user presses a button the background is blurred (animation) and a menu to choose a couple of things (which all go to a new viewController) is presented.

So basically there is a scene, the user opens the menu, the scene is blurred except for the menu and the user presses a button and another viewController is shown programmatically with a crossDissolve. This is how I show the new controller:

let storyboard = UIStoryboard.init(name: "Timer", bundle: nil)
let vc = storyboard.instantiateInitialViewController()!
vc.modalTransitionStyle = .crossDissolve
present(vc, animated: true, completion: nil)

So far so good, but I am not able to reset the original scene (basically to just remove the blur and reset the animated buttons). I have a function in it called setupScene() which prepares everything for the app start (some buttons are hidden, the blur has an alpha of 0, etc....).

I just dismiss the new viewController like this:

dismiss(animated: true, completion: nil)

And than the original scene with it's last state (blurred etc.) is shown again.

So when the new viewController is presented I would like to reset everything in the original viewController in the background, so that when the new viewController is dismissed the old scene is already reset, but this doesn't work. I tried to move the setupScene() function in viewDidAppear, but either in viewDidAppear or viewDidLoad first the new viewController is dismissed showing the original viewController in it's last state with the blur again and then the setupScene() runs to reset everything (which obviously works fine).

Is there a way to reset everything in the background while the new viewController is shown or while the crossDissolve is running?


Solution

  • This sounds like a good case for viewWillAppear. This is a lifecycle method that is called when the visibility of a view controller's views change. viewDidLoad is only called once.