2013-03-04 70 views
1

我试图创建一个核心数据,基于文档的应用程序,但有限制,一次只能查看一个文档(这是一个音频应用程序,wouldn'对于许多文档来说,立即产生噪音是有意义的)。如何子类NSDocumentController一次只允许一个文档

我的计划是以一种不需要将它链接到任何菜单的动作的方式来继承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"]]; 
} 

回答

0

我知道它已经有一段时间,但在情况下,它可以帮助别人....

我有什么,我认为是一个类似的问题,而解决办法是调用完成处理程序时,我的自定义DocumentController做打开文档,如:

- (void)openDocumentWithContentsOfURL:(NSURL *)url display:(BOOL)displayDocument completionHandler:(void (^)(NSDocument * _Nullable, BOOL, NSError * _Nullable))completionHandler { 

    if (doOpenDocument) { 
     [super openDocumentWithContentsOfURL:url display:displayDocument completionHandler:completionHandler]; 
    } else { 
     completionHandler(NULL, NO, NULL); 
    } 
} 

当我将它开始比单杆更多工作completionHandler(NULL, NO, NULL);