0

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?

  • Possible duplicate of [iPhone UIViewController init method not being called](http://stackoverflow.com/questions/772182/iphone-uiviewcontroller-init-method-not-being-called) – l'L'l Aug 09 '16 at 02:37
  • This question might also be helpful: http://stackoverflow.com/questions/2525438/viewcontroller-init – l'L'l Aug 09 '16 at 02:39
  • The list of initializers for framework classes is provided in the Class References for each class. One of the key features of inheritance in OOP languages is that methods need not be implemented in a subclass if the superclass's behavior is sufficient. This is why you don't see any `init` methods in a "default app". – Avi Aug 09 '16 at 04:15
  • how would I make a custom init method in my application then? I don't want to use the viewDidLoad method – Frost Zone Aug 09 '16 at 04:31

2 Answers2

0

You can use as below:

-(id)init
{
    self = [super init];
    if(self)
    {
        //add your customized code here..
    }

    return self;
}

Hope this helps!

Dharmesh Siddhpura
  • 1,610
  • 12
  • 22
0

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

Nandin Borjigin
  • 2,094
  • 1
  • 16
  • 37
  • If I wanted to init with another controller in my custom init, which one would I use? – Frost Zone Aug 09 '16 at 05:22
  • in short, implement the one you call. If there is no special requirement, `init` is sufficient. – Nandin Borjigin Aug 09 '16 at 05:39
  • So I would do it in the AppDelegate? Sorry for all the questions! – Frost Zone Aug 09 '16 at 05:46
  • The entrance of an iOS app is `application:didFinishLaunchingWithOptions: method` of an AppDelegate. What kind of codes you want to put into your initialization method? – Nandin Borjigin Aug 09 '16 at 06:08
  • you should probably read this: [The App Life Cycle ( Apple Official Documentation )](https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html) – Nandin Borjigin Aug 09 '16 at 06:08
  • I want to initialize another controller so that ViewController can use it. – Frost Zone Aug 09 '16 at 06:54
  • Assume that you want to use a viewcontroller B in viewcontroller A, we always initialize it in `viewDidLoad` method as long as there is no other class referring to that viewController B. In this case, initializing B in appDelegate is somehow not good, because no one except A wants to know about B – Nandin Borjigin Aug 10 '16 at 01:35
  • Yes that does sound good, but I would want a controller c getting controller A with controller B initialized in controller A. Does that make sense? – Frost Zone Aug 12 '16 at 04:44