我有一个使用NSWindowController子类的简单Cocoa应用程序。在笔尖中我设置了:
我的NSWindowController子类的init方法被调用(我称之为super),但不管我做什么都不会调用windowDidLoad。
我必须遗漏一些明显的东西,但对于我的生活,我无法弄清楚它是什么。
答案 0 :(得分:24)
您正尝试通过在另一个nib中实例化来创建NSWindowController
的实例。但是,当您在nib文件中实例化对象时,会通过调用-initWithCoder:
来初始化该对象。
-initWithCoder:
不是NSWindowController
的指定初始值设定项,因此NSWindowController
的实例永远不会实际加载其笔尖。
不是通过将NSWindowController
实例放在Interface Builder中的MainMenu.xib
文件中来实例化,而是以编程方式创建它:
在 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
答案 1 :(得分:14)
通过笔尖实例化窗口控制器是完全可以的。而不是使用windowDidLoad
作为您的钩子,在这种情况下,您将需要使用awakeFromNib
。
答案 2 :(得分:2)
可以根据需要加载窗口 - 尝试在window
中向自己发送-init
。有关详细信息,请参阅the discussion of -[NSWindowController loadWindow]
in the documentation。
答案 3 :(得分:0)
如果你写了
TTEst *test3 = (TTEst *)[[NSWindowController alloc] initWithWindowNibName:@"TTEst"];
尝试改为
TTEst *test3 = [[TTEst alloc] initWithWindowNibName:@"TTEst"];
它与众不同!当然第一行是错误的......