Quit app when NSWindow closes

自古美人都是妖i 提交于 2019-12-22 04:04:15

问题


How correctly to quit the Mac OS X app, when the main (the only one) closes?

I know there a method - (void)windowWillClose:(NSNotification *)notification in NSWindowDelegate. But it isn't quite suitable in my case, because it is called before NSWindow closes.


回答1:


You cannot have windowDidClose event since the notification that accompanies it would be holding an invalid object (the window is likely to have been deallocated on close). To achieve what you need, make your class the delegate of the Application, and implement the following method:

- (BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication *) theApplication;

From that method, return YES.

If your controller object has an instance in the MainMenu.nib, just make a connection from File's Owner (which means Application Object in the MainMenu.nob file). Control-Drag from File's Owner to your object, and connect the delegate outlet.

Or in source code, put something like this in your controller object's init method:

[NSApp setDelegate: self];


来源:https://stackoverflow.com/questions/3941960/quit-app-when-nswindow-closes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!