Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between viewDidLoad and viewDidAppear

Tags:

ios

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.

like image 530
user462455 Avatar asked Jun 29 '12 00:06

user462455


People also ask

What is the difference between viewDidLoad and viewDidAppear?

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.

Is viewDidAppear called after viewDidLoad?

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.

What is difference between viewWillAppear and viewDidAppear?

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.

Which is called first viewDidLoad or viewDidAppear?

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.


2 Answers

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

like image 166
davidgoli Avatar answered Sep 25 '22 05:09

davidgoli


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.

like image 42
WrightsCS Avatar answered Sep 22 '22 05:09

WrightsCS