Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
932 views
in Technique[技术] by (71.8m points)

cocoa - NSWindowController windowDidLoad not called

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...