10

I have a simple Cocoa app using a NSWindowController subclass. In the nib I have set:

  • File Owner's class to my NSWindowController subclass
  • The 'Window' outlet of the File's Owner to the main NSWindow in the nib.

The init method of my NSWindowController subclass is called (I call super), but not matter what I do windowDidLoad is never called.

I must be missing something obvious, but for the life of me I can't figure out what it is.

Mike Hornblade
  • 1,544
  • 2
  • 14
  • 19
  • How are you initializing the class? Are you calling super's implementation of `-initWithWindowNibName:`? – Rob Keniger Apr 23 '10 at 02:09
  • My NSWindowController subclass is instantiated within the nib, like this doc talks about: http://developer.apple.com/mac/library/documentation/cocoa/conceptual/ObjCTutorial/06Controller/06Controller.html#//apple_ref/doc/uid/TP40000863-CH8-SW1 Basically, MainMenu.xib creates it – Mike Hornblade Apr 23 '10 at 02:14
  • I faced similar kind of problem: I was not getting call to `- (void)windowDidLoad`. After some digging I found my mistake in my NSWindowController subclass i have declared `window` as property. I removed that member and called `showWindow:` method to get the window sucessfully. – Raviprakash Apr 13 '11 at 06:48

4 Answers4

26

You're trying to create the instance of NSWindowController by instantiating it in another nib. However, when you instantiate an object in a nib file, it is initialized by calling -initWithCoder:.

-initWithCoder: is not a designated initializer of NSWindowController, so your instance of NSWindowController never actually loads its nib.

Instead of instantiating your NSWindowController instance by placing it in the MainMenu.xib file in Interface Builder, create it programmatically:

In AppDelegate.h:

@class YourWindowController;
@interface AppDelegate : NSObject
{
    YourWindowController* winController;
}
@end

In AppDelegate.m:

@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification*)notification
{
    winController = [[YourWindowController alloc] init];
    [winController showWindow:self];
}
- (void)dealloc
{
    [winController release];
    [super dealloc];
}
@end

In YourWindowController.m:

@implementation YourWindowController
- (id)init
{
    self=[super initWithWindowNibName:@"YourWindowNibName"];
    if(self)
    {
        //perform any initializations
    }
    return self;
}
@end
Rob Keniger
  • 45,830
  • 6
  • 101
  • 134
14

It's perfectly fine to instantiate the window controller through a nib. Rather than use windowDidLoad as your hook, in that case you'll want to use awakeFromNib.

Demitri
  • 13,134
  • 4
  • 40
  • 41
2

The window might be loaded on demand - try sending window to yourself in -init. See the discussion of -[NSWindowController loadWindow] in the documentation for more info.

Jeremy W. Sherman
  • 35,901
  • 5
  • 77
  • 111
1

if you wrote

TTEst *test3 = (TTEst *)[[NSWindowController alloc] initWithWindowNibName:@"TTEst"];

try instead

TTEst *test3 = [[TTEst alloc] initWithWindowNibName:@"TTEst"];

it makes the difference ! Of course the first line was a mistake...

tontonCD
  • 320
  • 2
  • 6