2

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).

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Fabio Araujo
  • 55
  • 1
  • 7

1 Answers1

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.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • Hi, Peter Thank you by the answer. – Fabio Araujo Feb 13 '11 at 15:26
  • Hi, Peter Thank you by the answer. I've changed my source code a little bit, first the File's Onwer, now is type 'cadastroContas', I've linked all outlets to the File's Onwer. (So, my ControllerClass has no outlets linked). Now, it seens to be working fine, the init is called twice, but the dealloc is called twice too (so, I don't have a problem).. You said to delete the window controller that I created in the nib, could you explain more ? I din't realize what do you mean. – Fabio Araujo Feb 13 '11 at 15:35
  • @Fabio Araujo: The “cadastroContas” that you created in the nib. (You mention “my ControllerClass has no outlets linked”—that's the one you need to delete.) – Peter Hosey Feb 13 '11 at 20:00