为什么我要使用 NSObjectController?
虽然我搜索了很多关于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
绑定的好处/要点之一是消除代码。为此,NSObjectController 等的优点是它们可以直接在界面构建器中使用,并设置与各种 UI 元素的绑定。
绑定仅代表所提供的部分功能。 *ObjectController 类还可以自动处理应用程序通常需要的许多其他更重复的控制器(如模型、视图、控制器)代码。例如,它们可以:
如果您没有这样做,那么可能不值得使用 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:
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!
几天前,我在寻找有关 NSObjectController 的一些信息时读到了这个问题,今天在继续搜索时,我发现 以下段落似乎与问题相关:
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: