2013-03-04 69 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);