我有一个基于NSDocument的应用程序,我想在其中限制同时打开的文档数量(对于Lite版本).我只想拥有n个文档,如果用户尝试打开超过n个,则显示一条消息,其中包含指向完整应用程序下载的链接.
我已经设法使用NSDocumentController计算文档的数量,并且在readFromFileWrapper中,我可以返回FALSE.这会阻止新文档打开,但它会显示标准错误消息.我不知道如何避免这种情况.我想从笔尖打开一个新窗口.
有没有办法阻止NSDocument从readFromFileWrapper返回FALSE时显示标准错误消息?或者是否有任何其他方法可以在调用readFromFileWrapper之前阻止文档打开?
尝试使用此init方法,该方法在创建新文档和打开已保存文档时都会调用.如果达到限制,您只需返回nil.但是,我没有尝试过这个,它可能会导致显示相同的错误.
- (id)init {
if([[NSDocumentController documents] count] >= DOCUMENT_LIMIT) {
[self release];
return nil;
}
...
}
Run Code Online (Sandbox Code Playgroud)
如果显示相同的错误,您可以使用自定义NSDocumentController.您的实现将检查打开的文档的数量,在限制时显示消息,否则调用正常的实现.
- (id)openUntitledDocumentAndDisplay:(BOOL)displayDocument error:(NSError **)outError {
if([[self documents] count] >= DOCUMENT_LIMIT) {
// fill outError
return nil;
}
return [super openUntitledDocumentAndDisplay:displayDocument error:outError];
}
- (id)openDocumentWithContentsOfURL:(NSURL *)absoluteURL display:(BOOL)displayDocument error:(NSError **)outError {
NSDocument *doc = [self documentForURL:absoluteURL];
if(doc) { // already open, just show it
[doc showWindows];
return doc;
}
if([[self documents] count] >= DOCUMENT_LIMIT) {
// fill outError
return nil;
}
return [super openDocumentWithContentsOfURL:absoluteURL display:displayDocument];
}
Run Code Online (Sandbox Code Playgroud)