对子类 NSWindowController 的引用返回其文档 - 这是正确的吗?
我是基于文档的应用程序的新手,因此我可能错过了一些基本的东西。我编写了一个基于文档的应用程序,它使用子类 NSWindowController 作为接口,使用子类 NSDocument 作为模型。根据文档,我在 makeWindowControllers 中初始化 windowController 并加载其 xib。在界面生成器中,xib 将我的 windowController 子类设置为文件所有者。在窗口中的视图中,我有一个 NSOutlineView 的子类,并且 NSOutlineView 数据源和委托也在 nib 中引用并通过 IBOutlets 连接到 windowController。
根据文档,我应该能够通过 [windowController document] 从 OutlineView 数据源访问该文档。但是,从 OutlineView 数据源引用 windowController(通过 IBOutlet)会为我提供文档!
这导致 OutlineView 数据源(它是 windowController 的 xib 中 NSObject 的子类)中出现一些相当丑陋的代码来获取文档,例如:
-(MyDocument *)myDocument {
MyDocument *theDocument = (MyDocument *)myWindowController;
return theDocument;
}
头文件中的 IBOutlet 引用 myWindowController 的位置为:
IBOutlet MyWindowController *myWindowController
简而言之 - 为什么在这种情况下,连接到 windowController 的 IBOutlet 会直接获取文档吗?上面的代码可以工作,但似乎不应该。
编辑:澄清
I am new to document-based applications and hence I may have missed something fundamental. I have written a document based application which uses a subclassed NSWindowController for the interface and a subclassed NSDocument for the model. Per the documentation I initialise the windowController in makeWindowControllers and load its xib. In interface builder, the xib has my windowController subclass set as File's Owner. Among the views in the window, I have a subclass of NSOutlineView and the NSOutlineView datasource and delegate are also refenced in the nib and connected to the windowController via IBOutlets.
According to the documentation, I should be able to access the document from the OutlineView datasource via [windowController document]. However, referencing the windowController (via IBOutlet) from the OutlineView datasource gives me the document instead!
This has lead to some rather ugly code in the OutlineView datasoure (which is a subclass of NSObject in the windowController's xib) to get hold of the document, eg:
-(MyDocument *)myDocument {
MyDocument *theDocument = (MyDocument *)myWindowController;
return theDocument;
}
Where the IBOutlet in the header file references myWindowController as:
IBOutlet MyWindowController *myWindowController
In brief - why does an IBOutlet connected to the windowController get me the document directly instead in this situation? The above code works but seems as if it shouldn't.
Edit: clarification
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我找到了这个问题的答案 - 不要意外地将 xib 的文件所有者设置为 NSDocument,而不是代码另一部分中的 windowController,然后忘记你已经这样做了!这将覆盖您之前在 xib 中设置的文件所有者。
Okay, I worked out the answer to this one - don't accidentally set the File's Owner of the xib to the NSDocument instead of the windowController in another part of your code and forget that you did it! This overrides the File's Owner that you previously set in the xib.