NSWindowController windowDidLoad not called

强颜欢笑 提交于 2019-11-28 20:50:43

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

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.

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.

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...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!