假设我创建了一个新的Cocoa应用程序,并在创建项目时使用Document-based application
。它将有一个窗口,一个来自NSDocument
子类的窗口。如何才能使两个(或更多)窗口属于每个文档?
我创建了一个带有.xib文件的NSWindowController
子类,我创建了该接口。我该如何显示此窗口? NSWindowController
子类和NSDocument
子类之间的通信如何工作?
(我使用核心数据,所以它实际上是一个NSPersistentDocument
子类,但我认为这对于这个特定问题并不重要。)
答案 0 :(得分:2)
在您的NSDocument中
//Lazy instantiation of window controller
- (AdditionalWindowController *)additionalWC {
if (!_additionalWC) {
_additionalWC = [[AdditionalWindowController alloc] initWithWindowNibName:@"AdditionalWindow"];
}
return _additionalWC;
}
- (IBAction)openAdditionalWindow:(id)sender {
self.additionalWC.document = self;
[self.additionalWC showWindow:self];
}
或
- (IBAction)openAdditionalWindow:(id)sender {
//addWindowController ignores redundant invocations.
[self addWindowController:self.additionalWC];
[self.additionalWC showWindow:self];
}
在您的AdditionalWindowController中,您始终可以调用
id document = [self document];
//do what ever you want e.g. somethingDidChanged | direct method call of your document