Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't my awakeFromNib fire?

I am starting to move more of my view hierarchy construction to IB.

I have a nib file called "AlignmentViewController.xib" in which I set up my view hierarchy with AlignmentViewController as the files owner. This works fine.

One of the methods I remain fuzzy on is awakeFromNib. In the follow code snippet of AlignmentViewController I add the single additional method awakeFromNib. However, it does NOT get called:

- (void)awakeFromNib {

   NSLog(@"AlignmentViewController - awakeFromNib");
   [super awakeFromNib];

}

- (id)initWithChromosomeName:(NSString *)chromosomeName 
               basepairStart:(NSUInteger)basepairStart 
                 basepairEnd:(NSUInteger)basepairEnd {

    self = [self initWithNibName:@"AlignmentViewController" bundle:nil];

    if (nil != self) {


        self.title          = @"Alignment";

        self.chromosomeName = chromosomeName;
        self.basepairStart  = basepairStart;
        self.basepairEnd    = basepairEnd;

        self.wantsFullScreenLayout = YES;

    }

    return self;
}

Can someone clarify for me what I have missed here?

Cheers, Doug

UPDATE: After reading some the the answers I realize I need to get to the bottom of something that is pretty fundamental.

Looking at the initialization:

- (id)initWithChromosomeName:(NSString *)chromosomeName 
               basepairStart:(NSUInteger)basepairStart 
                 basepairEnd:(NSUInteger)basepairEnd {

    self = [self initWithNibName:@"AlignmentViewController" bundle:nil];

I notice I am calling initWithNibName:bundle: indirectly. Is this bad practice? The AlignmentViewController.xib file defines my view hierarchy. But I'm not really using initWithNibName:bundle: in the typical way? Is there a better way to do this?

like image 530
dugla Avatar asked Oct 19 '09 13:10

dugla


People also ask

What does awakeFromNib do?

awakeFromNib()Prepares the receiver for service after it has been loaded from an Interface Builder archive, or nib file.

When awakeFromNib is called?

awakeFromNib gets called after the view and its subviews were allocated and initialized. It is guaranteed that the view will have all its outlet instance variables set.


2 Answers

-awakeFromNib is sent to all object that are instantiated from the nib after all nib objects have instantiated. The owner is not one of those objects. It exists prior to deserializing the nib and is wired to the nib after loading, so it is not sent -awakeFromNib (unless of course it is instantiated by some other nib itself).

One clue here is that there's no way both -initFromChromosomePair:basepairStart:basepairEnd: and -awakeFromNib could both be called. The nib loader always calls -initWithCoder:, -initWithFrame: or -init (the rules for which is sent are in The Nib Object Life Cycle). The fact that you have some other designated initializer strongly suggests that you're manually creating this object somewhere in the code, which means this object is not "awaken from nib."

like image 140
Rob Napier Avatar answered Sep 21 '22 20:09

Rob Napier


Just adding a clarification: -awakeFromNib is sent to File's Owner in Cocoa, but not Cocoa Touch.

From Apple's Docs:

...

  • It sends an awakeFromNib message to the appropriate objects in the nib file that define the matching selector:
    • In Mac OS X, this message is sent to any interface objects that define the method. It is also sent to the File’s Owner and any proxy objects that define it as well.
    • In iPhone OS, this message is sent only to the interface objects that were instantiated by the nib-loading code. It is not sent to File’s Owner, First Responder, or any other proxy objects.
like image 21
dodgio Avatar answered Sep 19 '22 20:09

dodgio