在各种视图/笔尖之间共享用于绑定的对象(特别是 NSDocument)

发布于 2024-11-29 20:40:27 字数 523 浏览 5 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心头的小情儿 2024-12-06 20:40:27

如果您想绑定到它,您需要在其他类中引用某种文档。由于您已经在使用 NSViewController,因此您可以将 NSViewControllerrepresentedObject 设置为您的文档。或者,您可以子类化 NSViewController 并创建一个自定义属性来引用该文档。

YourViewController* controller = [[YourViewController alloc] initWithNibName:@"YourNib" bundle:nil];
[controller setRepresentedObject:yourDocument];

然后,在 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 the representedObject of the NSViewController to your document. Alternatively, you could subclass NSViewController and create a custom property to refer to the document.

YourViewController* controller = [[YourViewController alloc] initWithNibName:@"YourNib" bundle:nil];
[controller setRepresentedObject:yourDocument];

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>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文