gpt4 book ai didi

objective-c - 如何子类化 NSDocumentController 以一次只允许一个文档

转载 作者:行者123 更新时间:2023-12-03 17:13:14 27 4
gpt4 key购买 nike

我正在尝试创建一个基于核心数据的文档应用程序,但有一个限制,即一次只能查看一个文档(它是一个音频应用程序,对于大量文档发出噪音来说没有意义)立即)。

我的计划是以一种不需要将其链接到任何菜单操作的方式对 NSDocumentController 进行子类化。这一切进展顺利,但我遇到了一个问题,这让我对我的方法产生了一些疑问。

下面的代码在大多数情况下都有效,除非用户执行以下操作:- 尝试打开现有“脏”文档的文档- 单击保存/不保存/取消警报上的取消(这可以正常工作)- 然后再次尝试打开文档。由于某种原因,现在 openDocumentWithContentsOfURL 方法永远不会再次被调用,即使出现打开的对话框。

谁能帮我找出原因吗?或者给我举一个例子来说明如何正确地做到这一点?感觉像是一些人必须实现的东西,但我找不到 10.7+ 的示例。

- (BOOL)presentError:(NSError *)error
{
if([error.domain isEqualToString:DOCS_ERROR_DOMAIN] && error.code == MULTIPLE_DOCS_ERROR_CODE)
return NO;
else
return [super presentError:error];
}

- (id)openUntitledDocumentAndDisplay:(BOOL)displayDocument error:(NSError **)outError
{
if(self.currentDocument) {
[self closeAllDocumentsWithDelegate:self
didCloseAllSelector:@selector(openUntitledDocumentAndDisplayIfClosedAll: didCloseAll: contextInfo:)
contextInfo:nil];

NSMutableDictionary* details = [NSMutableDictionary dictionary];
[details setValue:@"Suppressed multiple documents" forKey:NSLocalizedDescriptionKey];
*outError = [NSError errorWithDomain:DOCS_ERROR_DOMAIN code:MULTIPLE_DOCS_ERROR_CODE userInfo:details];
return nil;
}

return [super openUntitledDocumentAndDisplay:displayDocument error:outError];
}

- (void)openUntitledDocumentAndDisplayIfClosedAll:(NSDocumentController *)docController
didCloseAll: (BOOL)didCloseAll
contextInfo:(void *)contextInfo
{
if(self.currentDocument == nil)
[super openUntitledDocumentAndDisplay:YES error:nil];
}

- (void)openDocumentWithContentsOfURL:(NSURL *)url
display:(BOOL)displayDocument
completionHandler:(void (^)(NSDocument *document, BOOL documentWasAlreadyOpen, NSError *error))completionHandler NS_AVAILABLE_MAC(10_7)
{
NSLog(@"%s", __func__);
if(self.currentDocument) {
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:[url copy], @"url",
[completionHandler copy], @"completionHandler",
nil];
[self closeAllDocumentsWithDelegate:self
didCloseAllSelector:@selector(openDocumentWithContentsOfURLIfClosedAll:didCloseAll:contextInfo:)
contextInfo:(__bridge_retained void *)(info)];
} else {
[super openDocumentWithContentsOfURL:url display:displayDocument completionHandler:completionHandler];
}
}

- (void)openDocumentWithContentsOfURLIfClosedAll:(NSDocumentController *)docController
didCloseAll: (BOOL)didCloseAll
contextInfo:(void *)contextInfo
{
NSDictionary *info = (__bridge NSDictionary *)contextInfo;
if(self.currentDocument == nil)
[super openDocumentWithContentsOfURL:[info objectForKey:@"url"] display:YES completionHandler:[info objectForKey:@"completionHandler"]];
}

最佳答案

Apple's cocoa-dev mailing list 上有非常丰富的信息交流。它描述了为了实现您的目的而对 NSDocumentController 进行子类化所必须执行的操作。结果是,当打开新文档时,现有文档会被关闭。

您可能会考虑的其他方法是在窗口退出主窗口时静音或停止播放文档(即,向窗口的委托(delegate)发送 NSWindowDidResignMainNotification ),如果只是为了避免强制执行看似对用户的人为限制。

关于objective-c - 如何子类化 NSDocumentController 以一次只允许一个文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15193136/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com