10

我有一个使用 NSWindowController 子类的简单 Cocoa 应用程序。在我设置的笔尖中:

  • 文件所有者的类到我的 NSWindowController 子类
  • 文件所有者的“窗口”出口到笔尖中的主 NSWindow。

我的 NSWindowController 子类的 init 方法被调用(我称之为 super),但无论我做什么 windowDidLoad 都不会被调用。

我一定错过了一些明显的东西,但对于我的生活,我无法弄清楚它是什么。

4

4 回答 4

26

您正在尝试NSWindowController通过在另一个 nib 中实例化它来创建实例。但是,当您在 nib 文件中实例化对象时,它会通过调用-initWithCoder:.

-initWithCoder:不是 的指定初始化程序NSWindowController,因此您的实例NSWindowController永远不会实际加载其 nib。

不要NSWindowController通过将实例放置MainMenu.xib在 Interface Builder 的文件中来实例化您的实例,而是以编程方式创建它:

AppDelegate.h中:

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

AppDelegate.m中:

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

YourWindowController.m 中

@implementation YourWindowController
- (id)init
{
    self=[super initWithWindowNibName:@"YourWindowNibName"];
    if(self)
    {
        //perform any initializations
    }
    return self;
}
@end
于 2010-04-23T03:54:16.070 回答
14

通过 nib 实例化窗口控制器是非常好的。而不是windowDidLoad用作你的钩子,在这种情况下你会想要使用awakeFromNib.

于 2012-02-19T20:22:51.573 回答
2

该窗口可能会按需加载 - 尝试window-init. 有关更多信息,请参阅文档中的讨论。-[NSWindowController loadWindow]

于 2010-04-23T01:58:58.400 回答
1

如果你写

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

试试吧

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

它有所作为!当然,第一行是错误的......

于 2016-07-06T13:15:44.410 回答