根据苹果的文档,如果你退出一个应用程序,closeAllDocumentsWithDelegate (来自NSDocumentController)应该为所有打开的文档调用canCloseDocumentWithDelegate of NSDocument。
在我的NSPersistentDocument-based应用程序中,我需要覆盖canCloseDocumentWithDelegate,以便警告用户,以防文档关闭时某个服务器功能仍在运行。这与任何数据更改都无关。这在用户关闭单个文档时起作用;我可以给出一个带有警告的工作表,让用户取消关闭过程。
但是,当应用程序退出时,我的canCloseDocumentWithDelegate版本不会被调用。那是什么原因呢?
发布于 2020-03-29 13:33:10
据苹果开发人员技术支持称,这是一个众所周知的问题。我最终切断了应用程序退出菜单项的自动接线,并自行处理。我需要从外部提供文档的服务器功能信息(在本例中是optionButton的状态),并将该函数添加到AppDelegate中。
- (IBAction)terminateGracefully:(id)sender {
BOOL optionOn = FALSE;
for (Document *doc in NSApp.orderedDocuments) {
if (doc.optionButton.state == NSControlStateValueOn) {
optionOn = TRUE;
}
}
if (optionOn) {
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@"Checkbox in some window is on"];
[alert setInformativeText:@"Something is going on. If you close the app now, it will stop. Close anyway?\n"];
[alert setAlertStyle:NSAlertStyleCritical];
[alert addButtonWithTitle:@"Don't close"];
[alert addButtonWithTitle:@"Close anyway"];
NSModalResponse resp = [alert runModal];
if (resp == NSAlertSecondButtonReturn) {
// We really want to close
[NSApp terminate:sender];
}
} else {
[NSApp terminate:sender];
}
}然后我将应用程序的退出菜单项绑定到terminateGracefully:

https://stackoverflow.com/questions/60038037
复制相似问题