0

All OS X application that support NSWindowRestoration can be closed by selecting the menu entry "Quit and Close All Windows" (Option-Command Q). This disables the state restoration and the next time you open the app all windows will be in their default position.

The menu entry triggers the terminate: method on NSApplication. But so does the regular "Close App" menu as well (Command Q).

How can I do the "Quit and Close All Windows" programmatically? Do I really have to close all windows by myself and then call terminate:?

How does Apple magically decide what to do, when both actions are connected to the same terminate: method?

4

1 回答 1

2

似乎没有一个很好的方法来做到这一点。您可能想向 Apple 提出请求(以及为什么需要它的解释)。

terminate:当两个动作都连接到同一个方法时,Apple 如何神奇地决定要做什么?

好吧,看看 AppKit 的反汇编,它似乎-[NSApplication terminate:]检查了发送者是否是NSMenuItem. 如果是,它检查它userInterfaceItemIdentifier是否等于@"NSAlternateQuitMenuItem"

我想,您可以使用该标识符创建一个虚拟菜单项并将其作为发送者传递给-terminate:,尽管由于这依赖于实现细节,它可能随时中断。

另一个控制因素是系统偏好设置>常规>“退出应用程序时关闭窗口”的设置。这对应于用户默认的 key NSAlternateQuitMenuItem,尽管这又是一个实现细节。看来您可以在调用之前设置它-terminate:,然后在-applicationWillTerminate:委托方法中删除该设置。(您的更改将与您的应用程序相关联。它们不会影响其他应用程序或系统偏好设置中的设置。)当然,您必须确保禁用突然终止以获取该委托方法调用。

于 2016-06-13T00:27:14.783 回答