Search code examples
objective-cmacoscocoanswindowcontroller

NSWindowController awakeFromNib is being called twice


I have an application that intends to create a popup window when a button is clicked. The popup window will load from a nib file. And so, the button is clicked and the window happily pops up. BUT, its awakeFromNib method gets called twice. Here's the code;

Application Delegate:

...

-(IBAction)myButton:(id)sender{

    printf("[settings]: button pressed\n");

    Config_SelectorSetup *selectorSetup = [[Config_SelectorSetup alloc] initWithWindowNibName:@"Config_SelectorSetup"];

    printf("about to load\n");

    [[selectorSetup window] makeKeyAndOrderFront:sender];

}

Config_SelectorSetup.m

- (id) initWithWindowNibName:(NSString *)windowNibName{

    printf("[initWithWindowNibName]\n");

    if( self = [super initWithWindowNibName:windowNibName] ){

        ...

    }

    return self;

}

- (void)awakeFromNib{

    printf("[awakeFromNib]\n");

    [self startScreen];
}

And here is the output:

[settings]: button pressed
[initWithWindowNibName]
about to load
[awakeFromNib]
[awakeFromNib]

Analyzing the call stack, first time it's called by [NSObject performSelector:] the second one by [NSIBObjectData nibInstantiateWithOwner:topLevelObjects:].

Can someone tell what am I doing wrong?

Thanks


Solution

  • Does Config_SelectorSetup.xib contain a Config_SelectorSetup object besides File's Owner?

    Try logging self in awakeFromNib -

    NSLog(@"self = %p", self);
    

    Does it print the same address each time? If it's printing different addresses, chances are you have a Config_SelectorSetup object in your nib.