Sankalp
Sankalp

Reputation: 2824

Interface builder: how to load a nib file

I am working on a project where I have created a .xib file and want to load the window created in the xib file.

I have tried the following code, but to no avail:

- (id) initWithWindowNibName: (NSString *) windowNibName
{
    self = [ super initWithWindowNibName: windowNibName];
    if ( self != nil )
    {
        [ self window ];

    }
    return self;
}

I am calling the initWithWindowNibName method hoping that it would load the window associated with the current controller. Basically, I am throwing darts in the dark!

I actually have very little idea about how to basically associate the created nib with the controller so that the above code actually loads the window. I have been able to associate various IBOutlets and IBActions but just not able to load the window.

Am I going the wrong path or is there specific method calls to load the window?

Edit: The [super initWithWindowNibName: windowNibName] call is to NSWindowController which is the super class of this controller class.

Upvotes: 0

Views: 484

Answers (2)

Peter Hosey
Peter Hosey

Reputation: 96323

Do I need to add the file to the Copy Bundle Resources in the Build Phases portion of the Xcode project settings ?

If by “the file” you mean the nib, then yes.

You need to add it to the Copy Bundle Resources phase for the compiled nib to be placed into your bundle's Resources folder. Otherwise, Xcode will not compile your xib nor place a nib of it in your bundle's Resources folder.

If your nib isn't in your bundle's Resources folder, then you will not be able to load anything from it.

The easiest way to add your nib to the Copy Bundle Resources phase is to select the nib in the Project Navigator, open the File Inspector, and check the box for the target you want to use the nib in.

Xcode prompts you for this information when you create a new nib (or a window controller or view controller with a nib included). One of the last steps is to choose which targets to add it to. Any time you create a new nib (directly or as a side effect), make sure you review that list and make sure the right set of boxes are checked.

Upvotes: 0

Anoop Vaidya
Anoop Vaidya

Reputation: 46533

You call it as :

self.myWindowController = [[NSWindowController alloc] initWithWindowNibName:@"NewWindow"];

Then you need to show the window:

[self.myWindowController showWindow:self]; //self or nil

Or if you want to load it as sheet:

[NSApp beginSheet: [self.myWindowController window]
   modalForWindow: self.window
    modalDelegate: self
   didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
      contextInfo: nil];

EDIT:

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        ...//other initiliasation codes here
    }

    return self;
}

Upvotes: 1

Related Questions