0

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.

Sankalp
  • 2,796
  • 3
  • 30
  • 43
  • What leads you to believe that the code you presented doesn't work? What does `[self window]` return? – Peter Hosey Dec 10 '13 at 00:38
  • @PeterHosey It returns an `NSWindow`. But, as I asked in the answer below, do I need to add the file to the `Copy Bundle Resources` in the `Build Phases` portion of the project settings? – Sankalp Dec 10 '13 at 03:53
  • Are you telling me the return type, or that it did not return `nil`? Because the latter is what is important. – Peter Hosey Dec 10 '13 at 04:53

2 Answers2

1

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;
}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • In your first code snippet, I think you mean `[[NSWindowController alloc] initWithWindowNibName:...]` – FluffulousChimp Dec 09 '13 at 08:57
  • @NSBum Yes, he did mean that only. – Sankalp Dec 09 '13 at 09:13
  • @Anoop Vaidya, @NSBum Can the reason for the window not getting be that the `initWithWindowNibName` expects the name of a `.nib` file while I am having a `.xib` file? – Sankalp Dec 09 '13 at 09:14
  • Nib and xib are not the same. @Sankalp: Your .xib is source code (in a sense); Xcode produces a .nib from it. The nib is what your app will load. – Peter Hosey Dec 10 '13 at 00:37
  • @AnoopVaidya Do I need to add the file to the `Copy Bundle Resources` in the `Build Phases` portion of the Xcode project settings ? – Sankalp Dec 10 '13 at 03:50
0

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.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370