viewDidLoad and awakeFromNib timing

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


Without being an expert, and following this posts, I realise than in a Tab Controller scenario, In the "child" view controller, the awakeFromNib method is executed when tab controller (parent) is loaded, but viewDidLoad only when its "Tab" is clicked.

And, therefore I understand that this can be used to load data only when a specific tab is selected (clicked)


I don't think you have to call awakeFromNib on your super class.

Check this.

Edit

I just ran a quick test, here's the results:

Scenario 1: MainWindow.Xib has a UIViewController subclass TestingAwakeFromNibViewController, Which has it's own Nib file TestingAwakeFromNibViewController.xib.

TestingAwakeFromNibViewController has an UIButton Outlet called btn3. Testing the following code :

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"Btn3 %@",btn3);

    NSLog(@"viewDidLoad");
}


-(void) awakeFromNib
{
    [super awakeFromNib];

    NSLog(@"Btn3 %@",btn3);

    NSLog(@"awakeFromNib");
}

Would Print:

Btn3 (null)
AwakeFromNib
Btn3 <UIRoundedRectButton: 0x64088e0; frame = (114 211; 72 37); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x6408890>>
ViewDidLoad

Scenario 2: Removing the xib file, adding a UIView as a son to the TestingAwakeFromNibViewController inside MainWindow.Xib, and adding UIButton as a subview to the UIView (and connecting the UIbutton outlet to the appropriate outlet of TestingAwakeFromNibViewController).

Now running the above code would print:

Btn3 <UIRoundedRectButton: 0x4e31c30; frame = (114 211; 72 37); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x4e31be0>>
viewDidLoad
Btn3 <UIRoundedRectButton: 0x4e31c30; frame = (114 211; 72 37); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x4e31be0>>
awakeFromNib

Meaning ViewDidLoad prior to AwakeFromNib.

Third Scenario: Same as the second, just without calling [super awakeFromNib];

Btn3 <UIRoundedRectButton: 0x4e0ddf0; frame = (114 211; 72 37); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x4e0dda0>>
awakeFromNib

Now ViewDidLoad is not even getting called.

So, it seems like different scenarios are calling for different action, and we need to prepare ourselves according to the one we're acting upon.


I create a test project with Navigation-based Application option, and add following codes to rootViewController.m.

- (void)awakeFromNib {
     NSLog(@"awakeFromNib 1");
     [super awakeFromNib];
     NSLog(@"awakeFromNib 2");
}

- (void)viewDidLoad {
     NSLog(@"viewDidLoad 1");
     [super viewDidLoad];
     NSLog(@"viewDidLoad 2");
}

Then, I got the results from console:

awakeFromNib 1
awakeFromNib 2
viewDidLoad 1
viewDidLoad 2

The -(void)viewDidLoad will be called when the view of controller is loaded. So, when the first time you use self.view = ..., the -(void)viewDidLoad will be invoke.


If you wrote something like followings, then -(void)viewDidLoad will be called first.

  - (void)awakeFromNib {
       NSLog(@"awakeFromNib 1");

       // The log sequence will be funny, if `viewDidLoad` raised before [super awakeFromNib]
       // If you are curios about it, just give it a try.           
       // self.view.backgroundColor = [UIColor clearColor];

       [super awakeFromNib];

       /// viewDidLoad will be called 
       /// because self.view must be loaded first.
       self.view.backgroundColor = [UIColor clearColor];  

       NSLog(@"awakeFromNib 2");
  }

and obtain following results.

awakeFromNib 1
viewDidLoad 1
viewDidLoad 2
awakeFromNib 2

Update

loadViewIfNeeded will trigger viewDidLoad if it loads view successfully. Sometimes I will call loadViewIfNeeded to ensure the @IBOutlet instances were initialized, and not null anymore.