0

I am writing a core data, document based app. I am trying to center the document's window. I have tried calling the following code from the document's makeWindowControllers method, from the window controller's awakeFromNib method and from the window controller's showWindows method. I keep getting a nil result for myWindow. Any ideas? Thank you.

NSWindow *myWindow = [myWindowController window];
[myWindow center];

1 Answers1

1

A common reason for -window to return nil is not having set the corresponding outlet in the nib file.

Make sure the nib file’s owner is your NSWindowController subclass and its window outlet has been linked to the window object.

  • And the problem has nothing to do with centering the window; you don't have a window to center. There may be a window somewhere that you would like to center, but either you have not loaded it or you have not hooked up an outlet to it. – Peter Hosey May 10 '11 at 06:58
  • Thank you both for the answer. I am new to Cocoa and it's these details I tend to miss. I thought outlets were only for when one needed to access UI elements directly. – Scott Henderson May 11 '11 at 03:36
  • @Scott That’s true in general. Classes like `NSWindowController` and `NSViewController` require you to set the window/view outlet when they operate on nib files, though. You could say that the outlets need to be set because these classes need them in code even though your code might not use them directly. –  May 11 '11 at 03:40