Frost Zone Frost Zone - 1 year ago 91
Objective-C Question

Where is the Init in a default app (Objective C)?

Hi this is my first question. I am very new to Objective C or more so on XCode. So I was making an app but I found that I could not find any init methods in my project of my default ViewController template. There was only a viewDidLoad.

I was wondering if I am supposed to make one? Or is there one I'm supposed to use? Also if I do make one how I'm supposed to make it run go through the init method? I tried one but it doesn't call the method at all when I run the app.

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

Why isn't the above code being called?

Answer Source

It depends.

There are several init functions to override and you don't need to implement all of them because only one of them would get called depending on how you initialize the ViewController instance.

  • if you call the init method (or its variant initWithFrame etc) directly, in AppDelegate for example

    ViewController* vc = [[ViewController alloc] init]; 

    then you can override the init method( or its variant, depending on which one you called)

  • if you hand over the initialization to IB(Interface Builder) the initWithCoder will called (I just remembered like this, not 100% sure).

  • if you initialize the ViewController by referring to a nib file, you should override the initWithNibName blablabla method

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download