Cocoa:在控制器中的windowDidLoad中获取窗口

发布于 2024-12-02 06:52:44 字数 394 浏览 0 评论 0原文

我的 NSWindowController 具有以下代码:

- (id)init {
    [self initWithWindowNibName:@"MyWindow"];       
    [self loadWindow];
    return self;
}

- (void)windowDidLoad
{
    [super windowDidLoad];

    NSWindow *window = [self window];
    NSAssert(window != nil, @"Can’t get window!");

    // do some stuff    
}

NSAssert 失败。

为什么?

我怎样才能得到窗户?

My NSWindowController has this code:

- (id)init {
    [self initWithWindowNibName:@"MyWindow"];       
    [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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

茶色山野 2024-12-09 06:52:44

这里有两个问题。首先,您的初始化程序缺少对 self 的分配:

- (id)init 
{
    self = [super initWithWindowNibName:@"MyWindow"];
    if(self)
    {
        [self loadWindow];
    }
    return self;
}

其次,您的断言失败的可能原因是您尚未连接 window 插座将 nib 文件中的文件所有者添加到窗口对象。这意味着您的窗口控制器不知道窗口属性指向哪个对象。

如果您不了解如何在 Interface Builder 中设置插座,那么您还有很多学习要做,您应该执行 简单教程 在你做任何其他事情之前,因为了解 Outlet 和 Action 的工作原理对于使用 Cocoa 进行编程至关重要。

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.

顾铮苏瑾 2024-12-09 06:52:44

不应该有一个吗?

self = [super init];

- (id)init {
[self initWithWindowNibName:@"MyWindow"];       
[self loadWindow];
return self;

你的}

Shouldn't there a

self = [super init];

in your

- (id)init {
[self initWithWindowNibName:@"MyWindow"];       
[self loadWindow];
return self;

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文