NSWindowController subClass - Init is Called twice

2019-07-18 20:30发布

问题:

im very new in cocoa development and I'm trying to load a Window. I will explain my problem.

When the user click the menuItem I use the following code to load my window

if ( !cadastroContasController )
{
    cadastroContasController = [[cadastroContas alloc]init];
    [cadastroContasController SetMenuItem:sender];
}
if ( ![[cadastroContasController window] isVisible] )
{
    NSLog(@"!isVisible");
    [cadastroContasController showWindow:nil];
}

I my cadastroContas class looks like this:

@interface cadastroContas : NSWindowController 
{
    NSMenuItem *mnuCommand;
    IBOutlet NSComboBox *cmbSelecao;
    IBOutlet NSTextField *txtNome;
    IBOutlet NSTextField *txtSaldoInicial;
    IBOutlet NSTextField *txtAnotacoes;
}


- (void)windowDidBecomeKey:(NSNotification *)notification;
- (BOOL)windowShouldClose:(id)sender;
- (void)windowWillClose:(NSNotification *)notification;
- (void)SetMenuItem:(NSMenuItem*) menu;
- (NSMenuItem*) MenuItem;

@end

and the implementation is

@implementation cadastroContas

-(void)windowDidLoad
{
NSLog(@"windowDidLoad");
[mnuCommand setState:NSOnState];
}

-(id)init
{
    self = [super initWithWindowNibName:@"cadastroContas"];
NSLog(@"Init self=%p", self);
return self;
}
-(void)dealloc
{
NSLog(@"Dealoc=%p", self);
[super dealloc];
}

- (void)windowDidBecomeKey:(NSNotification *)notification
{
NSLog(@"windowDidBecomeKey window=%p", [self window]);
}

- (BOOL)windowShouldClose:(id)sender
{
NSLog(@"windowShouldClose Window=%p", [self window]);
NSLog(@"mnuComando=%p GetMenuItem=%p", mnuCommand, [self MenuItem] );
if ( mnuCommand )
{
    [mnuCommand setState:NSOffState];
}
return YES;
}

- (void)windowWillClose:(NSNotification *)notification
{

NSLog(@"windowWillClose  Window=%p", [self window]);
NSLog(@"mnuCommand=%p GetMenuItem=%p", mnuCommand, [self MenuItem] );   
[self dealloc];
}

- (void)SetMenuItem:(NSMenuItem*) menu
{
mnuCommand = menu;
}

- (NSMenuItem*) MenuItem
{
    return mnuCommand;
}

@end

When the menu was clicked, I received two messages "Init" and I don't know why. Exemple:

[2223:a0f] Init self=0x10014fe40
[2223:a0f] Init self=0x10011f5a0

The second message let the "[cadastroContasController SetMenuItem:sender];" useless.

So, I need help to understand whats going on..

Another thing, [[cadastroContasController window] is always returning NULL(0x0)!!, but inside my controller i can handle it (it isn't null).

回答1:

This means you inited two instances, as shown by your logging of the self pointer: Notice that the value is different between the two messages.

You can use the Allocations instrument in Instruments to see what caused each window controller to be instantiated.

Usually, this problem happens when you create one of these in the nib and the other one in code. In the case of a window controller, the one you create in code should be the owner of its nib; you should not create another window controller as an object in the nib.

Another thing, [[cadastroContasController window] is always returning NULL(0x0)!!, but inside my controller i can handle it (it isn't null).

The window controller whose window outlet you set to the window is the one that is returning non-nil. The window controller whose window outlet you didn't set is the one that is returning nil.

Following from what I said above, after deleting the window controller you created in the nib, you should connect your File's Owner's window outlet to the window.