设置多个 NSWindowController 对象和 NSDocument

Setting up multiple NSWindowController objects and NSDocument

我是 NSDocument 架构的新手,我希望为单个文档设置多个 windows(以及多个 NSWindowController 对象)。

据我了解,NSDocument 确实是为处理单个 window 而创建的,而且似乎后来才硬塞给多个 windows 的能力。例如,NSDocument 似乎应该始终是任何 window 的 NIB 文件的文件所有者。但是,如果我想将 window 控制器与文档分开怎么办?

比如在NSDocument子类中我目前使用的代码是:

- (void)makeWindowControllers {
    [self setMyWindowController1:[[WindowControllerType1 alloc] initWithWindowNibName:@"MyWindow" owner:self]];
    [self addWindowController:[self MyWindowController1]];
}

但是 NIB 文件 "MyWindow" 的文件所有者设置为 NSWindowController 子类 (WindowControllerType1),而不是我的 NSDocument 子类。在这种情况下,每当我希望使用 [[NSDocumentController sharedDocumentController] currentDocument] 获取文档时,这总是 returns nil.

我认为如果我将 NIB 文件的所有者设置为 NSDocument 子类可以纠正这个问题,但是我的所有插座 link 都中断了,我不确定如何 link到 NSWindowController 子类 (WindowControllerType1),因为典型的操作过程(据我所知)是使 NSDocument 也成为 window 控制器委托,我想避免这种情况!

有什么建议吗?

编辑:

让我澄清一下并添加一些新信息。我知道 Apple 在使用 WindowController 文档 属性 上的立场。但是,由于我计划在每个 window 中有更多的嵌套 NSView,因此我想避免通过大量视图链传递文档以实现此目的。

不一定是这条链的问题。主要是当 [[NSDocumentController sharedDocumentController] currentDocument] 始终为 nil 时,NSDocument 的 "for free" 功能的 none 似乎起作用,例如 undo/redo。这是我需要解决的主要问题。

使用 NSWindowController 的文档 属性 而不是 currentDocument。

From what I understand, NSDocument was really created to work with a single window, and it seems that the ability to have multiple windows was shoehorned in later.

不,makeWindowControllers 在 OS X v10.0 及更高版本中可用。

But what if I wanted to separate the window controllers from the document?

window 控制器拥有 NIB。

Any suggestions?

[self setMyWindowController1:[[WindowControllerType1 alloc] initWithWindowNibName:@"MyWindow"]].

NSWindowController 有一个 属性 documentaddWindowController: 设置。