当主窗口关闭时关闭 Cocoa 中的子窗口

发布于 2024-08-06 22:19:20 字数 428 浏览 6 评论 0原文

我是 Cocoa 新手,所以我的方法很可能是错误的,但是..

我有一个应用程序,它使用 NSWindowController打开多个子窗口(在加载主/父窗口之后) >initNibWIthName:。这很好用。

但是,当我关闭父窗口(使用红色 x)时,它们仍然保持打开状态,并阻止应用程序关闭,直到它们也关闭为止。这是有道理的,因为我不会把它们关在任何地方。

但我该怎么做呢?此时肯定有一个事件被调用,但我在任何地方都找不到它是什么。

仅当应用程序实际终止时才会调用诸如 applicationWillTerminate (等等)之类的通知,而不是在按下关闭按钮时调用。

我想我正在寻找类似于 Windows WM_CLOSE 类型消息的内容。

I'm a Cocoa newbie so it is likely that my approach is wrong but ..

I have an app which opens several child windows (after the main/parent window has been loaded) using NSWindowController and initNibWIthName:. This works fine.

But when I close the parent window (using the red x) these remain open and prevent the app from closing until they are closed as well. This makes sense as I am not shutting them anywhere.

But how do I do this? There must be an event that is called at this point but I can't find what it is anywhere.

Notifications such as applicationWillTerminate (and so on) are only called when the application actually is terminating not when the close button has been pressed.

I guess I'm looking for something similar to the Windows WM_CLOSE type messages.

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

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

发布评论

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

评论(3

情深已缘浅 2024-08-13 22:19:20

您会发现最接近的等效项是窗口在关闭之前发布的 NSWindowWillCloseNotification。您可以使用以下方法让子窗口在父窗口关闭时自行关闭:

NSWindow *parentWindow;
NSArray *childWindows;

NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
for (NSWindow *childWindow in childWindows) {
  [noteCenter
   addObserver:childWindow selector:@selector(close)
   name:NSWindowWillCloseNotification object:parentWindow];
}

如果子窗口将在其父窗口之前被释放,请确保在此之前取消注册它的通知。

Mark 提到的委托方法是委托的一种便捷方法,可以省去注册他们可能想要的通知的麻烦。您不需要创建一个窗口控制器来接收该消息;只需发送窗口 [window setDelegate:myObject] 就会导致 myObject 接收 -windowWillClose: 消息(如果它响应该方法)。

顺便说一句,Cocoa 所说的“子窗口”与您的想法不同。 窗口编程指南,但是如果您查看 NSWindow 上相关方法的文档,您会发现它们基本上跟踪其父窗口的移动,以便它们随之移动。

如果您从 Win32 编程转向 Cocoa,您可能会发现 Apple 的 从 Windows Win32 API 移植到 Mac OS X 有助于突出 Win32 和 Cocoa 之间的概念差异。

The closest equivalent you'll find is the NSWindowWillCloseNotification posted by the window prior to its closing. You can probably get the child windows to close themselves when the parent window closes using:

NSWindow *parentWindow;
NSArray *childWindows;

NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
for (NSWindow *childWindow in childWindows) {
  [noteCenter
   addObserver:childWindow selector:@selector(close)
   name:NSWindowWillCloseNotification object:parentWindow];
}

If the child window will be deallocated before its parent, be sure to unregister it for notifications before that happens.

The delegate method mentioned by Mark is a convenience method for the delegate that saves them the trouble of registering for a notification they'll likely want anyway. You don't need to create a window controller just to receive that message; simply sending the window [window setDelegate:myObject] will cause myObject to receive the -windowWillClose: message if it responds to the method.

By the way, what Cocoa calls "child windows" differs from what you're thinking of. They're not addressed in the Window Programming Guide, but if you look at the documentation for the related methods on NSWindow, you'll see that they basically track the movements of their parent window, so that they move with it.

If you're coming to Cocoa from Win32 programming, you might find Apple's Porting to Mac OS X from Windows Win32 API helpful to highlight conceptual differences between Win32 and Cocoa.

糖粟与秋泊 2024-08-13 22:19:20

在 Mac OS X 中,Windows 和应用程序不是一回事。

如果您有一个单窗口界面,有一个主窗口,除了“关于”、“首选项”等之外没有其他窗口,那么您应该实现 applicationShouldTerminateAfterLastWindowClosed: 在您的应用程序委托中并返回 YES。这是关闭窗口导致应用程序退出的唯一方法(除了手动执行之外)。

如果您有一个多窗口界面(如在典型的基于文档的应用程序中),那么您应该使所有这些窗口彼此对等。检查器和工具选项板等窗口应该是浮动面板,而不是常规窗口。关闭最后一个窗口永远不应该退出这样的应用程序。

Windows and applications are not the same thing in Mac OS X.

If you have a single-window interface, with a main window and no others except for About, Preferences, etc., then you should implement applicationShouldTerminateAfterLastWindowClosed: in your application delegate and return YES. This is the only way (aside from you doing it manually) that closing a window causes the application to quit.

If you have a multiple-window interface (as in a typical document-based application), then you should make all of those windows peers to one another. Windows such as Inspectors and tool palettes should be floating panels, not regular windows. And closing the last window should never, ever quit such an app.

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