iphoneipaduitableviewviewdidloadawakefromnib

viewDidLoad and awakeFromNib timing


It is my understanding that awakeFromNib will always be called before viewDidLoad.

So I have a subclass of a UITableViewController, which is unarchived from a xib file.

I defined these two methods inside:

- (void)awakeFromNib {
  [super awakeFromNib];
  NSLog(@"awake from nib");
}

- (void)viewDidLoad {
  [super viewDidLoad];
  NSLog(@"view did load");
}

what happens is that "view did load" shows up before "awake from nib" in the console. I tried to use a breakpoint at [super awakeFromNib], and repeatedly hit F7 (Step Into), and to my surprise, it stepped into -(void)viewDidLoad BEFORE going to the second line inside awakeFromNib.

Does anyone have a clue what is going on here? I did the exact same thing in a subclass of a regular UIViewController, and the log statements are reversed, as I initially expected...


Solution

  • To understand that fact I recommend you to see loadNibNamed:owner:options: method of NSBundle.

    When you init a view controller from nib, first of all it loads views that are contained there, then it sets file owners properties according to nib. viewDidLoad method is called when it sets file owner's view property to one of the views that were loaded. And awakeFromNib is called when all file owners outlets and properties are set (including view property). So it makes sense that viewDidLoad is called earlier than awakeFromNib.

    Hope this'll help