显示 NSWindow
单击菜单项会显示一个 NSWindow,但只是第一次,为什么会这样,以及如何修复它?
-(IBAction)menuClick:(id)sender
{
if (!formsView) {
formsView = [[NSWindowController alloc] initWithWindowNibName:@"Forms"];
}
[formsView showWindow:self];
}
Clicking menu item shows a NSWindow, but only for first time, why is so that, and how to fix it?
-(IBAction)menuClick:(id)sender
{
if (!formsView) {
formsView = [[NSWindowController alloc] initWithWindowNibName:@"Forms"];
}
[formsView showWindow:self];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这与我使用的代码相同并且有效。问题一定出在其他地方。
问题不在于代码,而在于 Interface Builder 中的设置。 (我仍在使用 Xcode 3.2,所以在我的世界中 Interface builder 是一个单独的应用程序。)确保 xib 中的 File's Owner 类设置为 NSWindowController ,并且它的
window
插座连接到窗口。如果您不这样做,并且为窗口设置了启动时可见,您将得到您所描述的确切症状。这是因为
initWithWindowNibName
加载 NIB 文件,并且由于 Visible At Launch 标志而显示窗口。但是如果你查看NSWindowController
实例的window
属性,你会发现它仍然是 nil。这就是当您再次调用showWindow
时窗口不会重新显示的原因。 IB 中的连接将窗口与控制器关联起来。这是一个正确设置了 IB 的项目。
以下是 IB 中的相关设置:
That's the same code I use and it works. The problem must be elsewhere.
The problem isn't the code, it's the setup in Interface Builder. (I'm still using Xcode 3.2 so in my world Interface builder is a separate application.) Make sure the class for File's Owner in the xib is set to
NSWindowController
and that itswindow
outlet is connected to the window.If you don't do this and Visible At Launch is set for the window, you'll get exactly the symptom you describe. This is because
initWithWindowNibName
loads the NIB file and the window is shown because of the Visible At Launch flag. But if you look at thewindow
property of yourNSWindowController
instance you'll see that it is still nil. That's why the window isn't redisplayed when you callshowWindow
again. It's the connections in IB that associate the window with the controller.Here's a project that has IB set up correctly.
Here's the relevant settings in IB:
您是否在 Interface Builder(未内置于 Xcode 4 中)中取消选中窗口的“关闭时释放”复选框?
Did you uncheck the "release when closed" checkbox in Interface Builder (not built into Xcode 4) for the window?