My NSWindowController
has this code:
- (id)init {
[self initWithWindowNibName:@"M开发者_开发知识库yWindow"];
[self loadWindow];
return self;
}
- (void)windowDidLoad
{
[super windowDidLoad];
NSWindow *window = [self window];
NSAssert(window != nil, @"Can’t get window!");
// do some stuff
}
The NSAssert
fails.
Why?
How can I get the window?
There are two issues here. Firstly, your initialiser is missing the assignment to self
:
- (id)init
{
self = [super initWithWindowNibName:@"MyWindow"];
if(self)
{
[self loadWindow];
}
return self;
}
Secondly, and the probable reason that your assertion is failing, is that you have not connected the window
outlet of File's Owner in your nib file to the window object. This means that your window controller doesn't know what object the window property points to.
If you don't understand how to set outlets in Interface Builder then you have a lot of learning to do and you should do a simple tutorial before you do anything else, because understanding how outlets and actions work is fundamental to being able to program with Cocoa.
Shouldn't there a
self = [super init];
in your
- (id)init {
[self initWithWindowNibName:@"MyWindow"];
[self loadWindow];
return self;
}
精彩评论