从这个问题和你关于 NSArrayControllers 的相关问题,我收集到你正在做这样的事情:
- (void)makeWindowControllers
{
MyWindowController* wc = [[[MyWindowController alloc] initWithWindowNibName: [self windowNibName]] autorelease];
[self addWindowController: wc];
}
当你这样做时,-windowControllerDidLoadNib:
不会被调用,因为如果你以这种方式初始化,NSDocument 对象不是 Nib 的所有者。如果您查看,NSDocument.h
您会看到以下评论(请参阅添加的重点):
/* Create the user interface for this document, but don't show it yet. The
default implementation of this method invokes [self windowNibName],
creates a new window controller using the resulting nib name (if it is
not nil), **specifying this document as the nib file's owner**, and then
invokes [self addWindowController:theNewWindowController] to attach it.
You can override this method to use a custom subclass of
NSWindowController or to create more than one window controller right
away. NSDocumentController invokes this method when creating or opening
new documents.
*/
- (void)makeWindowControllers;
相反,如果您这样做:
- (void)makeWindowControllers
{
MyWindowController* wc = [[[MyWindowController alloc] initWithWindowNibName: [self windowNibName] owner: self] autorelease];
[self addWindowController: wc];
}
相信你会发现那-windowControllerDidLoadNib:
又叫了。如果您有充分的理由让 Nib 的所有者不是 NSDocument,那可能对您没有帮助,但这就是为什么-windowControllerDidLoadNib:
不被调用的原因,以及您可以采取哪些措施来恢复这种行为。这几乎肯定是比 init 更好的地方,这很可能发生在所有必要的 CoreData 支持东西到位之前。所以这是一种选择。