Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between viewDidAppear, viewDidLoad in iPhone/iOS? [duplicate]

Bottom line is, I've been working on an app, and it seems that if I place a UIAlert in viewDidLoad, it gets called twice (from a delegate method of UIImagePickerController). If I put it in viewDidAppear, it gets called once.

I've looked through documentation but it just confuses me.

like image 231
Sidwyn Koh Avatar asked Aug 29 '10 08:08

Sidwyn Koh


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.

What is the difference between viewDidLoad and viewWillAppear?

viewDidLoad ONLY gets called when the view is constructed - so for example after a view controller initFromNibNamed call when the view is accessed. viewWillAppear is called anytime your view controller was not in view but comes into view - so when your view controller is pushed, viewWillAppear is called.

What is viewDidAppear?

Notifies the view controller that its view was added to a view hierarchy. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.0+ tvOS 9.0+

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.


1 Answers

A UIView object can get loaded into memory and released multiple times without ever getting added to the view stack and appearing on the display.

My guess is that you have 2 references to this view (maybe one in a nib file?), so it's getting loaded, then released when the second reference is loaded and assigned to the same property, then only the latter gets added to the view stack. You can see this by printing out (NSLog) the integer value of self ("%ld",(long int)self) in the viewDidLoad and viewDidAppear methods.

like image 128
hotpaw2 Avatar answered Oct 05 '22 13:10

hotpaw2