NSDocumentController closeAllDocumentsWithDelegate不调用我的NSPersistentDocument子类的canCloseDocumentWithDelegate

问题描述 投票:0回答:1

根据Apple的文档,如果您退出应用程序,closeAllDocumentsWithDelegate(来自NSDocumentController)应为所有打开的文档调用canCloseDocumentWithDelegateNSDocument

在基于NSPersistentDocument的应用中,我需要重写canCloseDocumentWithDelegate,以便在文档关闭时某些服务器功能仍在运行时向用户发出警告。这与任何数据更改无关。当用户关闭单个文档时,此方法有效。我可以向工作表显示警告,并让用户取消关闭过程。

但是,退出应用程序后,我的canCloseDocumentWithDelegate版本不会被调用。这可能是什么原因?

macos cocoa nsdocument nspersistentdocument
1个回答
0
投票

根据Apple Developer技术支持,这是一个已知问题。最后,我切断了该应用程序退出菜单项的自动接线,并自己处理了所有这些。我需要从外部使文档的服务器功能信息可用(在此示例中,状态为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:

Xcode binding

© www.soinside.com 2019 - 2024. All rights reserved.