Reputation: 2207
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
Upvotes: 1
Views: 1191
Reputation: 2459
If you create an object from a nib and specify the NSWindowController as the owner, the window controller will get an awakeFromNib. For example, a common case is where the controller is a delegate for an NSTableView and the method
(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
is making the views like this,
return [tableView makeViewWithIdentifier:tableColumn.identifier
owner:self];
Note how self (the window controller) is passed as owner, which will cause it to see an awakeFromNib message every time this line is executed. In this case it's better to pass nil as the owner, and not rely on getting awakeFromNib for table cell views here.
I don't know what object is being created with your controller specified as the owner in your case, but this should put you on the right track.
Upvotes: 1
Reputation: 2480
How many outlets do you have in your class and what is that class subclassing? I found with certain subclasses (NSDocument
for instance), if you have multiple outlets connected, each nib object will fire the awakeFromNib
method upon loading. NSLog
your outlets to see if they output nil or an address.
Upvotes: 1
Reputation: 385500
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.
Upvotes: 3