tags:

views:

6441

answers:

3

I recently had a problem in my app where some of the subviews I was creating in a UIViewController subclass's -awakeFromNib method were disappearing from the view. After some poking around I found that moving the code I had put in -awakeFromNib to -viewDidLoad solved the problem. Seems that -awakeFromNib gets called only once when the UIViewController is unarchived from the nib, and -viewDidLoad gets called every time the view is unarchived.

So what's the best practice? It looks like UIViewController's -awakeFromNib shouldn't be adding any views to the view, that kind of stuff should be done in -viewDidLoad. Am I understanding this correctly? Or am I more confused than I thought?

+1  A: 

Yes, it's correct. You shouldn't really rely on awakeFromNib to do that kind of tasks.

awakeFromNib is similar to an event that's called after deserialization in .NET. viewDidLoad is similar to Load event in .NET.

If you are familiar with the concepts from .NET, this should be enough, I think.

Mehrdad Afshari
+13  A: 

awakeFromNib is called when the controller itself is unarchived from a nib. viewDidLoad is called when the view is created/unarchived. This distinction is especially important when the controller's view is stored in a separate nib file.

Kevin Ballard
A: 

Also important is that the awakeFromNib function will not be called never after memory warning appears. When recreating controller after memory warning calms down, only the viewDidLoad function will be called.

Prcela