关闭 Mac 应用程序(单击顶部的红十字)并通过单击扩展坞图标重新打开
当我关闭 Mac 应用程序(通过单击窗口顶部栏上的红十字按钮)时,应用程序图标保留在底部的扩展坞中。现在这是正常行为。当用户再次单击它时,它不会启动应用程序,除非用户完全退出应用程序并再次重新启动它。
Mac OS X 上的一个类似示例是“Activity Monitor”。您可以通过单击顶部的红十字按钮来关闭应用程序,但停靠图标仍保留在那里。用户可以通过单击停靠图标重新打开它。
我怎样才能在我自己的应用程序中实现这一目标?
When I close my Mac application (by clicking red cross button on window top bar) the app icon stays in the dock at the bottom. Now this is normal behaviour. When user click on it again it does not fire up the application unless the user quits the application altogether and relaunches it again.
A similar example on Mac OS X is "Activity Monitor". You can close the application by clicking the red cross button at the top the but dock icon stays there. User can re-open it by clicking dock icon.
How can I achieve this in my own application ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您仍然担心如何重新打开已关闭的窗口,请使用此方法:
您可以使用此方法来处理停靠栏中应用程序图标的点击。
有关更多信息,请查看 NSApplicationDelegate 协议参考。
这是文档:
http:// developer.apple.com/library/mac/#documentation/cocoa/reference/NSApplicationDelegate_Protocol/Reference/Reference.html
希望这有帮助!
最新更新:
在带有 Swift 5.2 的 MacOS 10.15 上的最新 Xcode 11.4 中,MacOS SwiftUI 应用程序中也存在同样的问题。在 AppDelegates.swift 中添加以下代码可以解决该问题。
If you are still concerned how to reopen the window that you have closed, use this method:
You can use this to handle clicks on the applications icon in the dock.
For further information check out the NSApplicationDelegate Protocol Reference.
Here is the documentation:
http://developer.apple.com/library/mac/#documentation/cocoa/reference/NSApplicationDelegate_Protocol/Reference/Reference.html
Hope this helps!
Latest Update:
In latest Xcode 11.4 on MacOS 10.15 with Swift 5.2, this same problem exists in MacOS SwiftUI app. Adding following code inside AppDelegates.swift solves the issue.
实现该方法
在您的应用程序委托中
您的应用程序将在窗口关闭后挂起,然后如果您
在应用程序委托中
实现,您可以在单击图标时执行一些操作,例如打开新窗口或旧窗口,如果您需要
查看http://developer.apple.com/library/ mac/#documentation/cocoa/reference/NSApplicationDelegate_Protocol/Reference/Reference.html 用于其他相关应用程序事件
Implement the method
in your app delegate
Your app will hang around after the window is closed and then if you implement
in the app delegate
You can do things when the icon is clicked such as open a new or old window if you need to
See http://developer.apple.com/library/mac/#documentation/cocoa/reference/NSApplicationDelegate_Protocol/Reference/Reference.html for other relevant application events
我认为上面的答案并不完全正确,要实现这一点,您应该覆盖
applicationShouldHandleReopen(_:hasVisibleWindows:)
https://developer.apple.com/reference/appkit/nsapplicationdelegate/1428638-applicationshouldhandlereopenI think that the answers above aren't fully correct, to achieve this you should override
applicationShouldHandleReopen(_:hasVisibleWindows:)
https://developer.apple.com/reference/appkit/nsapplicationdelegate/1428638-applicationshouldhandlereopen