I have an application that uses NSDocument to open files. I have an NSView in my NIB, and I’d like to set it’s size according to the image opened:
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError {
NSLog("FOO");
// Load image
// ...
// Change size
[mainView setFrameSize:NSMakeRect(image.size.width, image.size.height)];
[mainView display]; // Redraw
[image release];
return YES;
}
However, I did a little foobar check: ‘FOO’ is logged in readFromData:ofType:error: and ‘BAR’ is logged in the view’s initWithFrame: method, but the output in the console is:
2010-10-30 16:20:45.670 Pwnshop[513:a0f] Foo
2010-10-30 16:20:45.680 Pwnshop[513:a0f] Bar
Meaning that I’m sending the setFrameSize: message to an uninitiated NSView.
How can I make NSDocument load the nib first, and then do readFromData:ofType:error:, or better change the view size after the nib is loaded?
Thanks.
You can’t.
Edit from the year 2011: But if you can require Snow Leopard or later, you can override the
canConcurrentlyReadDocumentsOfType:class method to returnYES, in which case you will receive thereadFromData:ofType:error:message on a background thread. Read the linked documentation for the relevant requirements and caveats.Implement
windowControllerDidLoadNib:and make your window/view changes there. Don’t forget to call up tosuper, as demonstrated by the implementation provided by the NSDocument-subclass template.