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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1519548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:14:03  来源:igfitidea点击:

Closing child windows in Cocoa when the main window is closed

objective-cwindowscocoanswindowcontroller

提问by Peter Hosey

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

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

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

我有一个使用打开多个子窗口(主/父窗口加载后)的应用程序NSWindowControllerinitNibWIthName:。这工作正常。

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.

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

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.

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

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

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

回答by Jeremy W. Sherman

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

您将找到的最接近的等效项是NSWindowWillCloseNotification窗口在关闭之前发布的信息。您可以使用以下命令在父窗口关闭时让子窗口自行关闭:

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 myObjectto receive the -windowWillClose:message if it responds to the method.

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

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.

顺便说一句,Cocoa 所说的“子窗口”与您想象的不同。它们在Window Programming Guide 中没有解决,但是如果您查看有关方法的文档NSWindow,您会发现它们基本上跟踪其父窗口的移动,以便它们随之移动。

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

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

回答by user151019

回答by Peter Hosey

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

Windows 和应用程序在 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.

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

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.

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