macos - NSDocumentController closeAllDocumentsWithDelegate 不调用我的 NSPersistentDocument 子类的 canCloseDocumentWithDelegate
问题描述
根据 Apple 的文档,如果您退出应用程序, closeAllDocumentsWithDelegate
(from NSDocumentController
) 应该调用所有打开的文档。canCloseDocumentWithDelegate
NSDocument
在NSPersistentDocument
基于我的应用程序中,我需要覆盖canCloseDocumentWithDelegate
以警告用户,以防文档关闭时某些服务器功能仍在运行。这与任何数据更改无关。这在用户关闭单个文档时有效;我可以出示一张带有警告的工作表,让用户取消关闭过程。
但是,canCloseDocumentWithDelegate
退出应用程序时不会调用我的版本。这可能是什么原因?
解决方案
根据 Apple 开发人员技术支持,这是一个已知问题。我终于把app的退出菜单项的自动连线剪掉了,全部自己处理。我需要使文档的服务器功能信息从外部可用(在本例中为 的状态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: