Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to subclass NSDocumentController to only allow one doc at a time
    primarykey
    data
    text
    <p>I'm trying to create a Core Data, document based app but with the limitation that only one document can be viewed at a time (it's an audio app and wouldn't make sense for a lot of docs to be making noise at once).</p> <p>My plan was to subclass NSDocumentController in a way that doesn't require linking it up to any of the menu's actions. This has been going reasonably but I've run into a problem that's making me question my approach a little.</p> <p>The below code works for the most part except if a user does the following: - Tries to open a doc with an existing 'dirty' doc open - Clicks cancel on the save/dont save/cancel alert (this works ok) - Then tries to open a doc again. For some reason now the openDocumentWithContentsOfURL method never gets called again, even though the open dialog appears.</p> <p>Can anyone help me work out why? Or perhaps point me to an example of how to do this right? It feels like something that must have been implemented by a few people but I've not been able to find a 10.7+ example.</p> <pre><code>- (BOOL)presentError:(NSError *)error { if([error.domain isEqualToString:DOCS_ERROR_DOMAIN] &amp;&amp; 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"]]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload