3

I have been a big fan of this site for a long time, but this is my first posted question.

I have read every post similar to the topic (I think), however they all are using NIB files. I have NO nib files anywhere in my project.

It's a simple project with one view (so that mean one delegate class, one uiviewcontroller class, and one uiview class), and my view displays fine, except the viewDidLoad method is never called.

main.m looks like this:

int main(int argc, char *argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, @"MyAppDelegate");
    [pool release];
    return retVal;
}

MyAppDelegate.m looks like this:

-(void)applicationDidFinishLaunching:(UIApplication *)application{

     UIWindow *localWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

     self.window = localWindow;
     [localWindow release];

     MainViewController *viewController = [[MainViewController alloc] init];        

     //Option 1
     navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

     ////Option 2
     //navigationController = [[UINavigationController alloc] init];
     //navigationController.viewControllers = [NSArray arrayWithObject:viewController];

     ////Option 3
     //navigationController = [[UINavigationController alloc] init];
     //[navigationController pushViewController:viewController animated:NO];

     [window addSubview:navigationController.view];
     [window makeKeyAndVisible];

     [viewController release];
}

in MainViewController.m

- (id)init {
    self = [super init];
    if (self) {

        self.title = @"Root";

        MainView *myView = [[MainView alloc] init];
        [myView initWithParentViewController:self];

        self.view = myView;

        [myView release];

    }
    return self;
}

Finally, in "MainViewController.m", this is never fired (but the view is displayed):

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"VIEW DID LOAD");
}
a Caveman
  • 45
  • 1
  • 5

1 Answers1

5

If you have no nib file you should be implementing the loadView method. See "Creating the View Programmatically" in the Custom View Controllers section of the View Controller Programming Guide. If you don't implement this method viewDidLoad will not be called.

jbat100
  • 16,757
  • 4
  • 45
  • 70
  • In fact _not_ implementing that method tells iOS that it should be loading from a XIB file. Since it's not there the view is never loaded, hence the viewDidLoad method never being called. – Stephen Darlington Nov 23 '11 at 17:08
  • Just adding - (void)loadView { [super loadView]; } to the viewcontroller doesn't work. Are there other changes that I should be implementing? – a Caveman Nov 23 '11 at 19:16
  • You can see how to implement load view in the link, here is a SO Post http://stackoverflow.com/questions/5293976/how-to-implement-loadview – jbat100 Nov 23 '11 at 19:18
  • ok - forgive me, I am reading the link on a small laptop screen and missed it the first time. – a Caveman Nov 23 '11 at 19:22
  • So - looking back at my code example above, I was doing that in the init method instead of the loadView method. once i moved that code down to the loadView method, the viewDidLoad event started firing properly. Thanks for you help. – a Caveman Nov 23 '11 at 19:30