Question

I have the following method to open documents.

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
NSString *stringData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

[editorView setString:stringData];

if ( outError != NULL ) {
    *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
}

return YES;
}

It seems though that the document and its views are not loaded until YES is returned so I cant set the content of the text view because its not yet loaded. Does any one know of a work around for this?

Thanks in advance

Was it helpful?

Solution

Cocoa follows the model-view-controller (MVC) design pattern. You should read up on it in the conceptual documentation.

Don't store your model information in your view. That is, your document object should hold the contents of the loaded file (in an instance variable). Later, when if -windowControllerDidLoadNib is called (signifying a copy of the document UI has been loaded for the document), you can update your view with the file's contents.

NSDocument's approach is useful because there are a number of circumstances under which you might want to instantiate the document but not its UI (think scripting, importing, Spotlight importer querying, instant printing, ...).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top