views:

236

answers:

3

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?

+1  A: 

I imagine that in the cases where -viewDidUnload wasn't called, the view controller was released.

  1. viewDidLoad: controller loads view
  2. viewDidUnload: memory warning, controller unloads view
  3. viewDidLoad: controller loads view again
  4. -: controller gets released, doesn't explicitly unload the view

You and up with 2 -viewDidLoad calls and 1 `-viewDidUnload' call.

Maybe also put a NSLog into the -dealloc method and see if the number of -dealloc and -viewDidUnload calls combined matches the number of -viewDidLoad calls.

Thomas Müller
+7  A: 

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

vodkhang
Then what's the difference between viewDidUnload and didRecieveMemoryWarning?
Jonathan
First, The application receives a low-memory warning from the system. Then each view controller calls its didReceiveMemoryWarning method. Then, If the view controller releases its view, it calls its viewDidUnload method. You can override this method to perform any additional cleanup required for your views and view hierarchy.
vodkhang
Look at the link of Load and Unload cycle again
vodkhang
A: 

when a new view loads, the old view can still be loaded in the background. you are searching for viewWillAppear as conterpart i think.

views only unload in case of a memorywarning.

nico