NSWindowController subClass - Init被调用两次 [英] NSWindowController subClass - Init is Called twice
问题描述
im非常新的可可开发和我试图加载一个窗口。
我将解释我的问题。
im very new in cocoa development and I'm trying to load a Window. I will explain my problem.
当用户点击menuItem时,我使用以下代码加载我的窗口
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];
}
我的cadastroContas类看起来像这样:
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
,实现是
@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,我不知道为什么。
例如:
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
第二个消息让 [cadastroContasController SetMenuItem:sender];
The second message let the "[cadastroContasController SetMenuItem:sender];
" useless.
因此,我需要帮助来了解发生了什么。
So, I need help to understand whats going on..
另一件事, [[cadastroContasController窗口]
总是返回 NULL(0x0)
我可以处理它(它不是null)。
Another thing, [[cadastroContasController window]
is always returning NULL(0x0)
!!, but inside my controller i can handle it (it isn't null).
推荐答案
这意味着你引入了两个实例, self
指针:注意两个消息之间的值不同。
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.
您可以使用Allocations仪器看看什么导致每个窗口控制器被实例化。
You can use the Allocations instrument in Instruments to see what caused each window controller to be instantiated.
通常,当您在nib中创建其中一个,而在代码中创建另一个时,会发生此问题。在窗口控制器的情况下,您在代码中创建的应该是其笔尖的所有者;您不应在nib中创建另一个窗口控制器作为对象。
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.
另一件事,
[[cadastroContasController window]
总是返回NULL(0x0)
!!,但在我的控制器中我可以处理它(它不为null)。
Another thing,
[[cadastroContasController window]
is always returningNULL(0x0)
!!, but inside my controller i can handle it (it isn't null).
窗口控制器,其窗口
你设置到窗口的是一个返回非 - nil
。您未设置的窗口
插口的窗口控制器是返回 nil
的窗口控制器。
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
.
按照上面的说明,删除在nib中创建的窗口控制器之后,您应该将文件的所有者的窗口
outlet连接到窗口。
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.
这篇关于NSWindowController subClass - Init被调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!