当我的 Mac OS 应用程序退出时,我希望它询问用户“您确定要退出 [Yes] [No]”。
我试过这个:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
// Install a 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 {
// Insert YES/NO-dialog here
// if(blahahaha..
//[NSApp terminate:self];
}
但它只会捕捉到右键单击扩展坞上的应用程序然后选择“退出”时出现的退出。如果我按 Cmd-Q 或从应用程序菜单中选择退出,我的处理程序不会被调用...
最佳答案
并非所有这些案例都涉及 Apple 事件。相反,处理应用程序委托(delegate)方法-applicationShouldTerminate:
.
关于macos - 使用 NSAppleEventManager 捕获所有退出事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8620146/