views:

36

answers:

2

I have a class that extends NSWindowController and I am trying to position the window it controls. The window displays all of the expected contents and functions correctly, but when I try and position its starting location on the screen in the initWithWindowNibName method, the position does not change. Here is the code:

NSPoint p = NSMakePoint(100, 50);
[[self window] setFrameTopLeftPoint:p];

This seems very straight forward and I'm not sure what the problem is.

Thanks for any ideas.

(Found the problem. I did not have the window wired up to the Class in IB.)

A: 

Try putting that code in awakeFromNib.

Wevah
A: 

Wevah has the right idea, though I'll try to expand on it a bit.

If you were to try adding this line to your initWithWindowNibName: method:

NSLog(@"window == %@", [self window]);

You would likely see the following output to console:

window == (null)

In other words, the window is still nil, as init* methods are so early on in an object's lifetime that many IBOutlets or user interface items aren't quite "hooked up" yet.

Sending a message to nil is perfectly fine; it's simply ignored. So, basically your attempt to position the window has no effect because it basically equates to [nil doSomething];

The key then is to perform the positioning of the window later on in the controller object's lifetime, where the IBOutlets and other user interface objects are properly hooked up. As Wevah alluded to, one such method where things are properly hooked up is

- (void)awakeFromNib;

or in the case of NSWindowController, the following one as well:

- (void)windowDidLoad;

Hope this helps...

NSGod