views:

17

answers:

1

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.

+1  A: 

How can I make NSDocument load the nib first, and then do readFromData:ofType:error:, …

You can't.

… or better change the view size after the nib is loaded?

Implement windowControllerDidLoadNib: and make your window/view changes there. Don't forget to call up to super, as demonstrated by the implementation provided by the NSDocument-subclass template.

Peter Hosey