tags:

views:

2460

answers:

4

Hi folks, I am very new to iPhone programming and am running into a little bit of weirdness. For the following class, the init method just never gets called -- I have an NSLog function which should tell me when init is executed. Here's the relevant code:

@interface MyViewController : UIViewController {
}
@end

@implementation MyViewController
- (id) init
{
    NSLog(@"init invoked");
    return self;
}
@end

Any ideas as to what I am doing wrong -- if anything? Hopefully I provided enough information.

Thanks.

+1  A: 

Is the view coming up? Use these methods for additional initialization:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //...
}

// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad {   
    [super viewDidLoad];
    //..
}
Sean
Thanks! That worked. Though it leaves a little sour taste in my mouth to see some inconsistencies in Obj-C on iPhone. Oh well.
Daze
+4  A: 

You are probably creating your view controller from a NIB file. So, instead of calling "init" message, this is the one creator message being called:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}

Try if that is the one being called. What Sean said is true. You could use those messages to accomplish similar things.

Good luck.

Pablo Santa Cruz
You're absolutely correct. Thanks for the input!
Daze
Right after I posted my comment above, I realized why init wasn't being called... :P
Daze
A: 

Refer 'designated initializer' in reference document too.

Eonil
A: 

But, a UI component has sometimes severals init* methods, do we need to override all these methods in order to do some init. stuff?

eBuildy