Difference between viewDidLoad and viewDidAppear

Ios

Ios Problem Overview


What is the difference between viewDidLoad and viewDidAppear? What kind of initialization or custom code goes into those functions?

e.g. presentModalViewController works only when present in viewDidAppear and not on viewDidLoad.

Ios Solutions


Solution 1 - Ios

viewDidLoad is called exactly once, when the view controller is first loaded into memory. This is where you want to instantiate any instance variables and build any views that live for the entire lifecycle of this view controller. However, the view is usually not yet visible at this point.

viewDidAppear is called when the view is actually visible, and can be called multiple times during the lifecycle of a View Controller (for instance, when a Modal View Controller is dismissed and the view becomes visible again). This is where you want to perform any layout actions or do any drawing in the UI - for example, presenting a modal view controller. However, anything you do here should be repeatable. It's best not to retain things here, or else you'll get memory leaks if you don't release them when the view disappears.

See: https://developer.apple.com/documentation/uikit/uiviewcontroller

Solution 2 - Ios

Simply put, you would want to create any controls or arrays in viewDidLoad, where as in viewDidAppear is where you would want to refresh those controls or arrays.

viewDidLoad is called once when the controller is created and viewDidAppear is called each time the view, well, DID appear. So say you have a modal view that you present, when that view is dismissed, viewDidAppear will be called, and viewDidLoad will not be called.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser462455View Question on Stackoverflow
Solution 1 - IosdavidgoliView Answer on Stackoverflow
Solution 2 - IosWrightsCSView Answer on Stackoverflow