在各种视图/笔尖之间共享用于绑定的对象(特别是 NSDocument)
我正在使用 Xcode 4 为 OSX (Lion) 编写一个应用程序。
这个应用程序有点碰壁,而且到目前为止,互联网已经证明不是特别有帮助。
我定义了一个文档,以及 MainMenu.xib 和一个文档窗口;文档窗口当然是在加载/创建文档时创建的,因此可以访问相关数据。
我想在这个项目中尽可能多地利用 Cocoa Bindings,所以我的问题是:
如果我创建另一个视图(假设一个带有链接 Nib 的 NSViewController);我如何允许它访问文档的数据?
我尝试为文档类添加 #import,但是当我将文档类上的 #import 添加到视图控制器(以创建它)时,这会导致问题 - 我收到未知类型编译错误。
我还沿着传递临时对象的路线(甚至在创建视图控制器后访问 getters/setters);肯定有更好、更清洁的方法吗?
该应用程序只有一个 NSWindowController(默认),仅此而已,它是来自 XCode 非核心数据文档的应用程序模板的普通版本。
感谢您的帮助, 克林特
I'm writing an application for OSX (Lion) using Xcode 4.
Hitting a bit of a wall with this one and the internet has thus-far proved not particularly helpful.
I have a document defined, as well as the MainMenu.xib and a document window; The document window is of course what gets created when a document is loaded / created, and therefore has access to the relevant data.
I want to utilise Cocoa Bindings as much as possible in this project, so my question is this:
If I create another View (let's say an NSViewController with a linked Nib); how do I allow it access to the data for the document?
I've tried adding an #import for the document class, but that causes issues when I add the #import on the document class to the view controller (to create it) - I get unknown type compile errors.
I've also gone down the route of passing interim objects around (and even accessing getters / setters after creating the view controller); surely there must be a better, cleaner way?
The application has just the one NSWindowController (the default) and that is it, it's vanilla from the XCode non-core data document-based application template.
Thanks for your help,
Clint
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想绑定到它,您需要在其他类中引用某种文档。由于您已经在使用
NSViewController
,因此您可以将NSViewController
的representedObject
设置为您的文档。或者,您可以子类化NSViewController
并创建一个自定义属性来引用该文档。然后,在 Interface Builder 中,您可以使用
representedObject.<文档的某些属性>
的键路径将视图控制器 nib 中的对象绑定到文件所有者。You need to have a reference to the document of some kind in your other classes if you want to bind to it. Since you're already using an
NSViewController
you can set therepresentedObject
of theNSViewController
to your document. Alternatively, you could subclassNSViewController
and create a custom property to refer to the document.In Interface Builder you can then bind objects in your view controller nib to File's Owner using a key path of
representedObject.<some property of your document>
.