Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is viewDidUnload called less often than viewDidLoad?

I put NSLog(@"%@::%@", [[self class] description], NSStringFromSelector(_cmd)); in both viewDidLoad and viewDidUnload of a view controller.

In the log, I found viewDidLoad is called a lot more than viewDidUnload when the app moves to and from different .nibs.

Why?

like image 343
ohho Avatar asked Jul 26 '10 08:07

ohho


1 Answers

The viewDidLoad and viewDidUnload is not corresponding to each other.

The viewDidUnload will only be called when you receive a memory warning. The system then will automatically call your viewDidUnload.

In the normal case, when you push a MyViewController and pop it out. The life cycle will happens like this:

init

viewDidLoad

release

That means, whenever you init and push/present a view, a viewDidLoad will be called. But when you pop the view, the release will be called in normal case and viewDidUnload will be called in memory warning case.

This is quite implicit and Apple doesn't state it clearly on the Guide. Here is some reference: Load and Unload cycle

like image 196
vodkhang Avatar answered Nov 15 '22 17:11

vodkhang