私はココア開発で非常に新しく、ウィンドウを読み込もうとしています。私の問題を説明します。
ユーザーが 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」というメッセージが 2 つ表示されましたが、その理由はわかりません。例:
[2223:a0f] Init self=0x10014fe40
[2223:a0f] Init self=0x10011f5a0
2 番目のメッセージでは、" [cadastroContasController SetMenuItem:sender];
" が役に立たなくなりました。
だから、私は何が起こっているのかを理解するために助けが必要です..
もう1つは、[[cadastroContasController window]
常に!!を返すことですNULL(0x0)
が、コントローラー内で処理できます(nullではありません)。