关闭 Mac 应用程序(单击顶部的红十字)并通过单击扩展坞图标重新打开

发布于 2024-10-29 18:05:31 字数 238 浏览 4 评论 0原文

当我关闭 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 技术交流群。

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

发布评论

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

评论(3

你对谁都笑 2024-11-05 18:05:31

如果您仍然担心如何重新打开已关闭的窗口,请使用此方法:

- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag {

[window makeKeyAndOrderFront:self];

return YES;
}

您可以使用此方法来处理停靠栏中应用程序图标的点击。

有关更多信息,请查看 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 中添加以下代码可以解决该问题。

func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
    if !flag{
        window.makeKeyAndOrderFront(nil)
    }
    return true
}

If you are still concerned how to reopen the window that you have closed, use this method:

- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag {

[window makeKeyAndOrderFront:self];

return YES;
}

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.

func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
    if !flag{
        window.makeKeyAndOrderFront(nil)
    }
    return true
}
老旧海报 2024-11-05 18:05:31

实现该方法

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{ 
return NO; 
}

在您的应用程序委托中

您的应用程序将在窗口关闭后挂起,然后如果您

- (void)applicationDidBecomeActive:(NSNotification *)aNotification
{
//dock icon has just been clicked , or cmd-tabbed into
}

在应用程序委托中

实现,您可以在单击图标时执行一些操作,例如打开新窗口或旧窗口,如果您需要

查看http://developer.apple.com/library/ mac/#documentation/cocoa/reference/NSApplicationDelegate_Protocol/Reference/Reference.html 用于其他相关应用程序事件

Implement the method

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{ 
return NO; 
}

in your app delegate

Your app will hang around after the window is closed and then if you implement

- (void)applicationDidBecomeActive:(NSNotification *)aNotification
{
//dock icon has just been clicked , or cmd-tabbed into
}

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

风吹雨成花 2024-11-05 18:05:31

我认为上面的答案并不完全正确,要实现这一点,您应该覆盖 applicationShouldHandleReopen(_:hasVisibleWindows:) https://developer.apple.com/reference/appkit/nsapplicationdelegate/1428638-applicationshouldhandlereopen

I 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

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