开发者

Cocoa: get window in windowDidLoad in controller

开发者 https://www.devze.com 2023-03-31 22:45 出处:网络
My NSWindowController has this code: - (id)init { [self initWithWindowNibName:@\"M开发者_开发知识库yWindow\"];

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

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;

}

0

精彩评论

暂无评论...
验证码 换一张
取 消