【发布时间】:2015-10-18 16:28:06
【问题描述】:
我是 NSDocument 架构的新手,我希望为单个文档设置多个窗口(因此也有多个 NSWindowController 对象)。
据我了解,NSDocument 确实是为使用单个窗口而创建的,而且具有多个窗口的能力似乎是后来被硬塞进的。例如,似乎 NSDocument 应该始终是任何窗口的 NIB 文件的文件所有者。但是如果我想将窗口控制器从文档中分离出来呢?
例如在NSDocument子类中我目前使用的代码:
- (void)makeWindowControllers {
[self setMyWindowController1:[[WindowControllerType1 alloc] initWithWindowNibName:@"MyWindow" owner:self]];
[self addWindowController:[self MyWindowController1]];
}
但 NIB 文件“MyWindow”的文件所有者设置为 NSWindowController 子类 (WindowControllerType1),而不是我的 NSDocument 子类。在这种情况下,每当我希望使用 [[NSDocumentController sharedDocumentController] currentDocument] 获取文档时,它总是返回 nil。
我认为如果我将 NIB 文件的所有者设置为 NSDocument 子类,则可以纠正此问题,但随后我的所有出口链接都断开了,并且我不确定如何链接到 NSWindowController 子类 (WindowControllerType1),作为典型行动方案(据我所知)是使 NSDocument 也成为窗口控制器委托,我想避免这种情况!
有什么建议吗?
编辑:
让我澄清并添加一些新信息。我知道 Apple 对使用 WindowController 的文档属性的立场。但是,由于我计划在每个窗口中拥有大量嵌套的 NSView,因此我希望避免通过大量视图链来传递文档以实现此目的。
问题不一定是这个链条。主要是当[[NSDocumentController sharedDocumentController] currentDocument] 始终为零时,NSDocument 的“免费”功能似乎都不起作用,例如撤消/重做。这是我需要解决的主要问题。
【问题讨论】:
-
无需通过视图层次结构传递文档。您可以访问 view.window.windowController.document (并最终将 viewController.represented 对象设置为整个文档模型的子模型)。关于 currentDocument 问题,我在这里发布了一个替代解决方案:stackoverflow.com/questions/8912314/…
标签: cocoa nsdocument nswindowcontroller