Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to NSWindow Xib after loading it?

In my code below, CustomWindow is a subclass of NSWindow.

CustomWindow *window = [[CustomWindow alloc] init];
if (![NSBundle loadNibNamed:@"NibName" owner:window])
[window center]; // doesn't work

How do you get a pointer to control your XIB after you load it so you can do things such as centering the NSWindow (I mean the serialised one that resides inside the XIB)?

What am i doing wrong here?

like image 341
Brock Woolf Avatar asked Mar 11 '10 19:03

Brock Woolf


2 Answers

You should be using an NSWindowController subclass. NSWindowController is specifically designed to do exactly what you want to achieve and solves several problems that you will run into if you load the nib directly using the methods of NSBundle. You generally should always use an NSWindowController subclass to manage windows.

Create a subclass of NSWindowController:

@interface MyWindowController : NSWindowController {}
@end

@implementation MyWindowController
- (id)init
{
    self = [super initWithWindowNibName:@"MyWindow"];
    if(self)
    {
        //initialize stuff
    }
    return self;
}
//this is a simple override of -showWindow: to ensure the window is always centered
-(IBAction)showWindow:(id)sender
{
    [super showWindow:sender];
    [[self window] center];
}
@end

In Interface Builder, set the class of File's Owner to be MyWindowController and connect the window outlet of File's Owner to the window object in your nib.

You can then display the window by doing this:

MyWindowController* controller = [[MyWindowController alloc] init];
[controller showWindow:self];
like image 90
Rob Keniger Avatar answered Sep 29 '22 21:09

Rob Keniger


In my code below, CustomWindow is a subclass of NSWindow.

CustomWindow *window = [[CustomWindow alloc] init];
if (![NSBundle loadNibNamed:@"NibName" owner:window])
[window center]; // doesn't work

How do you get a pointer to control your XIB after you load it so you can do things such as centering the NSWindow inside the XIB?

“centering the NSWindow inside the XIB” makes no sense (you would center it on the screen), unless you mean centering the NSWindow object that is inside the xib, in which case, why are you creating another NSWindow (CustomWindow) object outside of the xib?

Remember that a nib (or xib) is an archive of objects. If you want to use a window that you have in your nib, you need to create an outlet to point to that window, set the class of the File's Owner to be the class where you've added the outlet, hook up the outlet in IB, and appoint the object with the outlet as the File's Owner by passing it to the owner: argument. That object, as the owner, will then be responsible for working with the window. It may be (usually is, in my code) the same object that loads the nib.

Also, init doesn't work on NSWindow; you must use initWithContentRect:styleMask:backing:defer: or initWithContentRect:styleMask:backing:defer:screen:. Using init would only be valid if you've implemented init yourself in CustomWindow, and used one of those two selectors for the [super init…] message.

like image 34
Peter Hosey Avatar answered Sep 29 '22 22:09

Peter Hosey