Search code examples
swiftuiviewcontrollerviewdidload

Swift Call ViewDidLoad Manually


I'm having a problem with my app. I am saving data in the top ViewController and coming back to main ViewController. My problem is that when I dismiss the top view, viewDidLoad for the main view isn't called again. Since I'm saving information in my second view, and my first view needs to update when dismissed, I need viewDidLoad to be called. If you have any ideas on how to do this, any help is appreciated. I've seen the same question for Objective C, but not for swift

I've tried to find answers, but nothing helped


Solution

  • You should never, ever try to call UIViewController lifecycle methods manually. Those methods should only be called by the system.

    If you need to execute something every time your view controller appears on the screen, use viewWillAppear or viewWillAppear instead.

    viewDidLoad is only called 1x for the lifetime of each UIViewController instance, after the controller's view has been loaded to memory.

    For more information on UIViewController lifecycle, see Looking to understand the iOS UIViewController lifecycle.