10.6 之前如何让应用程序在最后一个窗口关闭时退出

发布于 2024-12-05 12:47:27 字数 421 浏览 2 评论 0原文

我正在尝试创建一个需要针对 Mac OS X 10.5 的 Cocoa 应用程序。我创建了一个新的 Cocoa 应用程序,它会自动为我生成第一个表单。

如果我构建,它将运行良好,直到我将基础 SDK 更改为 10.5,此时构建失败并显示“找不到 NSApplicationDelegate 的协议声明”。

现在,我知道 NSApplicationDelegate 是 10.6 的一个功能。我搜索并发现其他人只是说删除 NSApplicationDelegate 协议。我这样做了,它将构建并运行,但我无法让应用程序退出。如果我单击红色按钮关闭窗口,它会关闭,但停靠图标仍然存在。

我怀疑这与表单未获取/处理退出事件有关;然而,我是 Xcode 的新手,所以我不确定窗口代码应该是什么样子,并且查找 10.5 的示例源代码已被证明很棘手。

谁能帮助我吗?提前致谢。

I'm trying to create a Cocoa app that needs to target Mac OS X 10.5. I create a new Cocoa app and it auto-generates the first form for me.

If I build, it'll work well, until I change my base SDK to 10.5, at which point the build fails with a "Cannot find protocol declaration for NSApplicationDelegate."

Now, I know that NSApplicationDelegate was a 10.6 feature. I've searched and found others who simply say to remove the NSApplicationDelegate protocol. I do that and it will build and run, but I can't get the app to exit. If I click the red button to close the window, it closes but the dock icon remains.

I suspect it has something to do with the form not getting/handling the exit event; however, I'm new at Xcode so I'm not sure what the window code should look like and finding sample source for 10.5 has proven tricky.

Can anyone help me? Thanks in advance.

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

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

发布评论

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

评论(1

洋洋洒洒 2024-12-12 12:47:27

现在,我知道 NSApplicationDelegate 是 10.6 的一个功能。我搜索并发现其他人只是说删除 NSApplicationDelegate 协议。我这样做了,它将构建并运行,但我无法让应用程序退出。如果我单击红色按钮关闭窗口,它会关闭,但停靠图标仍然存在。

这与您是否声明符合该协议无关(该协议在 10.6 之前的 SDK 中不作为正式协议存在,这就是您无法使用它的原因)。

在 Mac 上,窗口和应用程序是两个不同的事物,因此关闭窗口和退出应用程序同样是两个不同的事物。应用程序拥有窗口,因此退出应用程序将关闭其所有窗口(但是,如果应用程序支持 Lion 的状态恢复功能,则它们将在应用程序下次启动时返回)。关闭窗口不会退出应用程序。

但是,对于单窗口应用程序,关闭应用程序的主窗口以退出应用程序确实有意义。系统偏好设置和许多其他单窗口应用程序证明了这一点。

为此,请实现 NSApplication 委托协议中的方法。

请注意我写的方式。该协议在 10.6 之前并不是正式协议,但它仍然存在,如 非正式协议。你不能声明与它的一致性,但你仍然可以实现它的方法,并且 NSApplication 仍然会发送它的委托委托消息。

您需要应用程序的委托通过返回 YES 来响应 applicationShouldTerminateAfterLastWindowClosed:

Now, I know that NSApplicationDelegate was a 10.6 feature. I've searched and found others who simply say to remove the NSApplicationDelegate protocol. I do that and it will build and run, but I can't get the app to exit. If I click the red button to close the window, it closes but the dock icon remains.

That isn't related to whether you declare conformance to that protocol (which doesn't exist as a formal protocol in SDKs older than 10.6, which is why you couldn't use it).

On the Mac, windows and applications are two different things, so closing a window and quitting an application are likewise two different things. Applications own windows, so quitting an application will close all of its windows (but, if the app supports Lion's state-restoration feature, they will come back when the application is next launched). Closing a window does not quit the application.

However, for a single-window application, it does make sense for closing the application's primary window to quit the application. System Preferences and numerous other single-window apps demonstrate this.

To do that, implement a method from the NSApplication delegate protocol.

Notice the way I wrote that. The protocol isn't a formal protocol before 10.6, but it does still exist—as an informal protocol. You can't declare conformance to it, but you can still implement its methods, and NSApplication will still send its delegate delegate messages.

You need your application's delegate to respond to applicationShouldTerminateAfterLastWindowClosed: by returning YES.

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