NSWindowController subClass - Init is Called twice

六眼飞鱼酱① 提交于 2019-12-11 04:13:03

问题


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.



来源:https://stackoverflow.com/questions/4981913/nswindowcontroller-subclass-init-is-called-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!