Cocoa:在控制器中的windowDidLoad中获取窗口
我的 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 NSAssert
fails.
Why?
How can I get the window?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有两个问题。首先,您的初始化程序缺少对
self
的分配:其次,您的断言失败的可能原因是您尚未连接 的
window
插座将 nib 文件中的文件所有者添加到窗口对象。这意味着您的窗口控制器不知道窗口属性指向哪个对象。如果您不了解如何在 Interface Builder 中设置插座,那么您还有很多学习要做,您应该执行 简单教程 在你做任何其他事情之前,因为了解 Outlet 和 Action 的工作原理对于使用 Cocoa 进行编程至关重要。
There are two issues here. Firstly, your initialiser is missing the assignment to
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
in your
}