我正在开发 cocoa 应用程序。我的应用程序最初显示一个弹出表。我需要知道当我们尝试通过右键单击并选择停靠栏图标上的“退出”来退出应用程序时会触发哪个事件,因为由于弹出表而无法退出应用程序。我该如何解决这个问题?
最佳答案
当在 Dock 菜单中选择“退出”项时,您的应用会收到一个 quit
Apple 事件。如果您想拦截此事件,则需要为此事件安装自定义 Apple 事件处理程序。请注意,工作表在工作表被关闭之前阻止应用程序终止是正常的,因此,如果您更改此行为,您的应用程序的工作方式将与其他应用程序不同。
以下是如何在应用程序委托(delegate)中覆盖 quit
Apple 事件的默认处理程序的简单示例:
- (void)applicationDidFinishLaunching:(NSNotification*)notification
{
//install the custom quit event handler
NSAppleEventManager* appleEventManager = [NSAppleEventManager sharedAppleEventManager];
[appleEventManager setEventHandler:self andSelector:@selector(handleQuitEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEQuitApplication];
}
//handler for the quit apple event
- (void)handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
{
[self terminate:self];
}
关于objective-c - 应用程序退出事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1768497/