NSWindow 关闭时退出应用程序
当主应用程序(唯一的应用程序)关闭时,如何正确退出 Mac OS X 应用程序?
我知道 NSWindowDelegate
中有一个方法 - (void)windowWillClose:(NSNotification *)notification
。但它不太适合我的情况,因为它是在 NSWindow
关闭之前调用的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能拥有
windowDidClose
事件,因为伴随该事件的通知将持有无效对象(窗口可能在关闭时已被释放)。要实现您的需要,请将您的类设为应用程序的委托,并实现以下方法:从该方法中返回
YES
。如果您的控制器对象在
MainMenu.nib
中有一个实例,只需从文件所有者(即 MainMenu.nob 文件中的应用程序对象)建立连接即可。按住 Control 键并从文件所有者拖动到您的对象,然后连接委托插座。或者在源代码中,将类似的内容放入控制器对象的 init 方法中:
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: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: