2

我在可可开发方面非常新,我正在尝试加载一个窗口。我会解释我的问题。

当用户单击 menuItem 时,我使用以下代码加载我的窗口

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

我的 cadastroContas 类如下所示:

@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

并且实现是

@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

单击菜单时,我收到两条消息“Init”,我不知道为什么。示例:

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

第二条消息让“ [cadastroContasController SetMenuItem:sender];”没用了。

所以,我需要帮助来了解发生了什么..

另一件事,[[cadastroContasController window]总是返回NULL(0x0)!!,但在我的控制器内部我可以处理它(它不是空的)。

4

1 回答 1

1

这意味着您启动了两个实例,如self指针的日志记录所示: 请注意,两条消息之间的值不同。

您可以使用 Instruments 中的 Allocations 工具来查看导致每个窗口控制器被实例化的原因。

通常,当您在 nib 中创建其中一个而在代码中创建另一个时,就会发生此问题。对于窗口控制器,您在代码中创建的应该是其 nib 的所有者;您不应该在 nib 中创建另一个窗口控制器作为对象。

另一件事,[[cadastroContasController window]总是返回NULL(0x0)!!,但在我的控制器内部我可以处理它(它不是空的)。

您将其window出口设置为窗口的窗口控制器是返回非nil. window您未设置其出口的窗口控制器是正在返回的窗口控制器nil

按照我上面所说的,在删除您在笔尖中创建的窗口控制器后,您应该将文件所有者的window插座连接到窗口。

于 2011-02-13T03:36:34.467 回答