Tutorial :Who should call viewDidLoad on progammatically loaded views?


When I need to load a view programatically I do the following:

MyController* myController = [[MyController alloc] init];  [[NSBundle mainBundle] loadNibNamed:@"myNib" owner:myController options:nil];  // use my controller here, eg. push it in the nav controller  

This works fine, but my controller's viewDidLoad is never called. So I resorted to manually calling it after the loadNibNamed call, but it doesn't seem correct. I was expecting the framework to call the viewDidLoad on my behalf. Is this the right way or I'm missing something?


You should load view controllers with

MyController* myController = [[MyController alloc] initWithNibName:@"myNib" bundle:nil];  

Make sure your MyController extends from UIViewController, and the View property is set properly in Interface Builder.


I am new to Stack Overflow, but I discovered this question and discovered a couple other methods to load a nib file that ensure that the viewDidLoad method gets called automatically. Ed Marty's answer is correct, but it requires you to go outside the ViewController's code to load the nib file, these two examples I offer here allow you to keep your code inside the ViewController's implementation. Not necessarily better ways, just different. If you know of any drawbacks to doing it this way, please let me know your thoughts.

First, inside the initWithNibName:bundle: method of your UIViewController subclass, you can replace the call:
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

with something like this:
self = [super initWithNibName:@"NameOfAlternateNibFile" bundle:nibBundleOrNil];

Or, you can accomplish what appears to do exactly the same thing by the following:

[[NSBundle mainBundle] loadNibNamed:@"NameOfAlternateNibFile" owner:self options:nil];  [self setView:self.view]; //this line ensures that the viewDidLoad method is called  

The key is in understanding what is written in the comments above the function definition for $initWithNibName:bundle: in the UIViewController.h file (included at the bottom of my answer here, see italics).

The nice thing about doing this using either of these methods is that viewDidLoad gets called in either scenario.

Here are the directives listed in UIViewController.h:

The designated initializer. If you subclass UIViewController, you must call the super implementation of this method, even if you aren't using a NIB. (As a convenience, the default init method will do this for you, and specify nil for both of this methods arguments.) In the specified NIB, the File's Owner proxy should have its class set to your view controller subclass, with the view outlet connected to the main view. If you invoke this method with a nil nib name, then this class' -loadView method will attempt to load a NIB whose name is the same as your view controller's class. If no such NIB in fact exists then you must either call -setView: before -view is invoked, or override the -loadView method to set up your views programatically. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;


The views are loaded lazyly by UIViewController. If you use the accessor myController.view in your code, the view should be loaded and viewDidLoad be called.


I noticed the same thing. I think ViewDidLoad must be called by the view CONTROLLER. Since you don't have a view controllr in your nib, you have to call the viewdidload manually. I"m having to do the same thing.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »