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
.
The difference between viewDidAppear and viewDidLoad is that viewDidAppear is called every time you land on the screen while viewDidLoad is only called once which is when the app loads.
viewDidLoad is called after your view is loaded. It is called only once when view is initialized and pushed or presented. viewDidAppear is called once you see the loaded view on screen. It is called after view appeared.
The viewWillAppear method is called before loading the actual view. The viewDidAppear method is called when the view is already loaded, and you want to show something.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With