views:

356

answers:

4

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?

A: 

What makeds you think it wasn't called? You should never call this method from your code

ennuikiller
debugger break point doesn't fire. Also the code in it was obviously not run (adds button, button is MIA).
Remus Rusanu
+1  A: 

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.

pgb
Nope, that does seem to be the case either. The loaded view displays fine yet the viewDidLoad is never called. I can see in debugger stack that on a 'ordinary' controller loaded from a nib the viewDidLoad is called by the [UIViewController view], but is doesn't seem to apply to 'hand made' controllers.
Remus Rusanu
+1  A: 

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.

Ed Marty
A: 

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.

Ray