开发者

How to make an application quit when its last window is closed before 10.6

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-gener开发者_StackOverflow社区ates 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜