首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSDocumentController closeAllDocumentsWithDelegate不调用NSPersistentDocument子类的canCloseDocumentWithDelegate

NSDocumentController closeAllDocumentsWithDelegate不调用NSPersistentDocument子类的canCloseDocumentWithDelegate
EN

Stack Overflow用户
提问于 2020-02-03 11:11:40
回答 1查看 81关注 0票数 0

根据苹果的文档,如果你退出一个应用程序,closeAllDocumentsWithDelegate (来自NSDocumentController)应该为所有打开的文档调用canCloseDocumentWithDelegate of NSDocument

在我的NSPersistentDocument-based应用程序中,我需要覆盖canCloseDocumentWithDelegate,以便警告用户,以防文档关闭时某个服务器功能仍在运行。这与任何数据更改都无关。这在用户关闭单个文档时起作用;我可以给出一个带有警告的工作表,让用户取消关闭过程。

但是,当应用程序退出时,我的canCloseDocumentWithDelegate版本不会被调用。那是什么原因呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-29 13:33:10

据苹果开发人员技术支持称,这是一个众所周知的问题。我最终切断了应用程序退出菜单项的自动接线,并自行处理。我需要从外部提供文档的服务器功能信息(在本例中是optionButton的状态),并将该函数添加到AppDelegate中。

代码语言:javascript
复制
- (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:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60038037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档