objective-c - 应用程序退出事件

标签 objective-c cocoa macos appleevents

我正在开发 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/

相关文章:

ios - Objective-c:在 NSArray 上没有得到正确的文本

objective-c - NSArray 中没有重复的所有可能组合

objective-c - 在 NSPrintInfo 中设置文件名

python - OSX 上的 tar : Unrecognized archive format error when trying to unpack flower_photos. tgz、TF 教程

multithreading - 为什么performSelector是:onThread:withObject:waitUntilDone: not recommended for frequent inter-thread communication

macos - 通过 AppleScript 更改 Mac 壁纸

iphone - 清除空闲单元格连接 : found one to purge conn = 0x1d57ba00

UITableViewCell 和 heightForRowAtIndexPath 的 iOS 动态高度

xcode - 在 Xcode 中使用 NSTask 在 AppKit 应用程序中运行时,Rust 1.4.0(或更高版本)cargo/rustc 崩溃

cocoa - 如何在 Objective-C 中获取加载了默认配置文件的 bash 实例