init method not called

Discussion in 'iOS Programming' started by hervey, Nov 2, 2009.

  1. macrumors newbie

    Joined:
    Sep 23, 2009
    #1
    The init method is NOT called on the second snippet of code, but is called on the first snippet. I don't understand.

    Code:
    @implementation testAERAppDelegate
    @synthesize window;
    
    -(id)init {
    	[super init];
    	NSLog(@"init"); ...
    return self;
    }
    
    Code:
    @implementation RootViewController
    @synthesize pickerView,dateFormatter;
    
    -(id)initWithCoder {
    	[super init];
    	NSLog(@"init");
    	return self;
    }
     
  2. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #2
    Your method isn't called init
     
  3. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #3
  4. thread starter macrumors newbie

    Joined:
    Sep 23, 2009
    #4
    My Bad! I copied over other code I was trying out. The correct posting of the code that's NOT working is:

    Code:
    @implementation RootViewController
    @synthesize pickerView,dateFormatter;
    
    -(id)init {
    	[super init];
    	NSLog(@"init");
    	return self;
    }
    
    Sorry for the confusion. Thanks for your help.
     
  5. macrumors 6502

    Joined:
    Apr 24, 2003
    #5
    How are you instantiating this class? Can you post the code where you are using the class?

    Is this RootViewController from inside a XIB/NIB? If so, it technically doesn't get init'ed when the app is started, but rather gets unarchived from the XIB. When this happens, the awakeFromNib method is called.

    Basically the rule of thumb, is if the class is being instantiated from code, init is called, but if the class is being instantiated from Interface Builder, then you want awakeFromNib.
     
  6. thread starter macrumors newbie

    Joined:
    Sep 23, 2009
    #6
    One from xib, one from code. Thank you for helping clarify this basic element, and the clear manner you made it clear for me. I have been studying Cocoa since March, and this basic element had escaped me.

    Cheers.
     

Share This Page