Wednesday, June 10, 2009

UIViewController ViewDidLoad

I just wanted to share my experience with UIViewController ViewDidLoad method in iPhone SDK.

Most of the people including me(used to) think that the UIViewController ViewDidLoad method is called only once when the Controller object is created and added in the Super View (or given a tag etc). But what i experienced that this method might get called multiple times. Sorry to disappoint you.

When ViewDidLoad method of your UIViewController is called, it renders your view and saves a copy of it in iPhone cache. This cached view is not removed from cache even you remove your view from the super view. So when you display this view again, it does not call ViewDidLoad of your UIViewController.

As you know that there is didReceiveMemoryWarning method in a UIViewController. When the iPhone gets low in memory it calls this function of the view that is having largest amount of memory. And if you have called the same function of the super class like this:

[super didReceiveMemoryWarning];

then your rendered view will be removed from the iPhone cache.
So before your view will appear on the iPhone screen next time, its ViewDidLoad will be called again to render it.

So you should avoid doing all your initialization and important stuff in the ViewDidLoad otherwise you will have problems in the above mentioned scenario.

I hope this post helps you during your iPhone development.

No comments:

Post a Comment