为什么我要使用 NSObjectController?

发布于 2024-12-23 04:42:25 字数 996 浏览 3 评论 0原文

虽然我搜索了很多关于 Cocoa Bindings 的信息,但我对自己所掌握的信息仍然比较不满意。似乎这个话题对很多人来说有点麻烦,很多人只是回避这种模式,我认为这是不应该的。

当然,绑定有时看起来可能太复杂,或者设计的开销太大......

但是,我有一个非常直接和具体的问题:如果我可以直接建立绑定,为什么还需要 NSObjectController?

例如,代码:

[controller bind:@"contentObject" toObject:self withKeyPath:@"numberOfPieSlices" options:nil];

[slicesTextField bind:@"value" toObject:controller withKeyPath:@"content" options:nil];
[stepperControl bind:@"value" toObject:controller withKeyPath:@"content" options:nil];

与以下代码完全相同:

[slicesTextField bind:@"value" toObject:self withKeyPath:@"numberOfPieSlices" options:nil];
    [stepperControl bind:@"value" toObject:self withKeyPath:@"numberOfPieSlices" options:nil];

在我的例子中,我们正在讨论发生所有事情的类的属性,所以我猜测何时需要 NSObjectController:

  • 控制器的关键路径是对象和其他控件的绑定需要到它的属性,而不是它的值,就像它们周围的基元和包装器一样(在我的例子中,numberOfPiesSlices是 NSInteger)

  • 或者当需要从其他外部对象进行绑定时,而不仅仅是在一个对象之间

需要有人能确认或拒绝这一点吗?

Although I have searched for many information about Cocoa Bindings, I still remain relatively unsatisfied with information I have and got. It seems that topic is somewhat troublesome for many and many are just avoiding this pattern, which I believe should not be.

Of course, it may seem that bindings are sometimes too complicated or perhaps designed with too much overhead...

However, I have one very direct and specific question: Why is NSObjectController needed if I can establish bindings directly?

For example, the code:

[controller bind:@"contentObject" toObject:self withKeyPath:@"numberOfPieSlices" options:nil];

[slicesTextField bind:@"value" toObject:controller withKeyPath:@"content" options:nil];
[stepperControl bind:@"value" toObject:controller withKeyPath:@"content" options:nil];

Does exactly the same as:

[slicesTextField bind:@"value" toObject:self withKeyPath:@"numberOfPieSlices" options:nil];
    [stepperControl bind:@"value" toObject:self withKeyPath:@"numberOfPieSlices" options:nil];

In my case here, we are talking about property of the class inside which everything is happening, so I am guessing the need for NSObjectController is when:

  • key path for controller is object and binding of other controls is needed to its properties, not to its value as with primitives and wrappers around them is the case (numberOfPiesSlices in my case is NSInteger)

  • or when binding is needed from other outside objects, not only between objects within one

Can anybody confirm or reject this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

终止放荡 2024-12-30 04:42:25

绑定的好处/要点之一是消除代码。为此,NSObjectController 等的优点是它们可以直接在界面构建器中使用,并设置与各种 UI 元素的绑定。

绑定仅代表所提供的部分功能。 *ObjectController 类还可以自动处理应用程序通常需要的许多其他更重复的控制器(如模型、视图、控制器)代码。例如,它们可以:

  • 连接到您的核心数据存储并执行必要的获取、插入和删除
  • 管理撤消/重做堆栈
  • 拾取已编辑但未提交到 UI 的更改并保存它们(例如,如果窗口关闭而焦点仍然存在)在编辑的文本字段上 - 这对我来说是一个新字段,我从 mmalc 在下面的线程中的答案中找到了它)。

如果您没有这样做,那么可能不值得使用 NSObjectController。它的子类(NSArrayController 等)更有用。

另请参阅此处对您的具体问题进行讨论!

One of the benefits/points of bindings is to eliminate code. To that end, NSObjectController etc. have the benefit that they can be used directly in interface builder and set up with bindings to various UI elements.

Bindings only represent part of the functionality on offer. The *ObjectController classes can also automatically take care of a lot of the other more repetitive controller (as in Model, View, Controller) code that an application usually needs. For example they can:

  • connect to your core data store and perform the necessary fetches, inserts and deletes
  • manage the undo / redo stack
  • Pick up edited but not committed changes to your UI and save them (e.g. if a window is closed while focus is still on an edited text field - this was a new one to me, I found it from mmalc's answer in the thread below).

If you're doing none of this, then it probably isn't worth using NSObjectController. Its subclasses (NSArrayController etc) are more useful.

Also see here for a discussion of your exact question!

恰似旧人归 2024-12-30 04:42:25

如果我可以直接建立绑定,为什么还需要 NSObjectController?

几天前,我在寻找有关 NSObjectController 的一些信息时读到了这个问题,今天在继续搜索时,我发现 以下段落似乎与问题相关:

如果绑定的对象实现了,会有好处
NSEditor注册。这就是为什么绑定是个好主意的原因之一
到控制器对象而不是直接绑定到模型。
NSEditorRegistration 让绑定告诉控制器它的
内容正在编辑中。控制器跟踪
其中视图当前正在编辑控制器的内容。如果
用户关闭窗口,例如,与关联的每个控制器
该窗口可以告诉所有此类视图立即提交它们的
待编辑,因此用户不会丢失任何数据。 Apple 提供了一些通用控制器对象(NSObjectController、
NSArrayController、NSTreeController)可用于包装您的
模型对象,提供编辑器注册功能。

使用
控制器还具有绑定系统所没有的优点
直接观察你的模型对象 - 所以如果你替换你的模型
对象与一个新对象(例如在详细视图中,用户已经
更改了正在检查的记录),您只需替换
控制器内的模型对象、KVO 通知和绑定
更新。

Why is NSObjectController needed if I can establish bindings directly?

I read this question a few days ago while looking for some information about NSObjectController, and today while continuing my search, I found the following passage which seemed relevant to the question:

There are benefits if the object being bound to implements
NSEditorRegistration. This is one reason why it’s a good idea to bind
to controller objects rather than binding directly to the model.
NSEditorRegistration lets the binding tell the controller that its
content is in the process of being edited. The controller keeps track
of which views are currently editing the controller’s content. If the
user closes the window, for example, every controller associated with
that window can tell all such views to immediately commit their
pending edits, and thus the user will not lose any data. Apple supply some generic controller objects (NSObjectController,
NSArrayController, NSTreeController) that can be used to wrap your
model objects, providing the editor registration functionality.

Using
a controller also has the advantage that the bindings system isn’t
directly observing your model object — so if you replace your model
object with a new one (such as in a detail view where the user has
changed the record that is being inspected), you can just replace the
model object inside the controller, KVO notices and the binding
updates.

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