子类化 NSArrayController 的原因是什么?
我正在尝试改进我的 KVC/KVO/Cocoa-Bindings-fu,并且想知道子类化 NSArrayController 的原因是什么?
I am trying to improve my KVC/KVO/Cocoa-Bindings-fu and was wondering what could be the reasons to subclass the NSArrayController?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我有一个自定义的 NSArrayController 子类,可以执行一大堆任务。我选择在那里实现这些东西是因为这样我就可以享受绑定和其他东西的全部舒适感。这就是我现在使用它的目的:
是的,等等。基本上,这一切都归结为这个本质:如果您想要输出的数据与输入的数据不同,则子类化 NSArrayController
Max
I have a custom NSArrayController subclass that perform a whole bunch of task. I chose to implement these things there because I can then enjoy the full comfort of bindings and stuff. Here's what I use this now for:
Yeah, and so on. Basically this all boils down to this essence: subclass NSArrayController if you want different data out than you put in
Max
在使用具有表视图的数组控制器时,我喜欢做的一件事是重写
add:
以发布通知,以便选择新项目并立即打开进行编辑。事实上,我不久前在 CocoaDev 上发布了这个:当然,可以对不是 NSArrayController 子类的控制器执行类似的操作;这只是我想到的第一个方法。
One thing I like to do when using an array controller with a table view is to override
add:
to post a notification so that the new item is selected and open for editing right away. I actually posted this over at CocoaDev a while ago:Of course it's possible to do similar with a controller that's not an
NSArrayController
subclass; this is just the first way I figured out.我有一个应用程序需要在用户添加对象时设置隐藏文件名 - 自定义 ArrayController 类中的 add 方法正是执行此操作的地方。
编辑 - 实际上,重新阅读我的 Hillegas,覆盖 newObject 是更好的方法。但它仍然需要 NSArrayController 的子类。
I have an app that needs to set a hidden file name when the user adds an object - the add method in a custom ArrayController class is just the place to do this.
Edit - Actually, re-reading my Hillegas, overriding newObject is the better way. It still requires a subclass of NSArrayController though.
我对数组控制器进行了子类化,以在调用 -(id)newObject; 时返回所需的对象;
通常你有 .h &项目中每个类的 .m 文件和数组控制器通过读取这些文件根据类名称创建特定对象。
但是,当您只有一个 .h .m 文件或一个类(例如:Entity),它可以根据您的需要返回任何对象(例如:员工、客户,通过读取存储的模式定义)时,您必须对 arraycontroller 进行子类化,因为类名无论您需要员工对象还是客户对象,都保持相同(实体)。
I subclassed array controller to return the desired object upon invoking -(id)newObject;
Normally you have .h & .m file for each class in your project and array controller creates specific object based on the name of the class by reading those files.
But when you have only one .h .m file or a class (eg:Entity) which can return any object (eg: employee,customer, by reading stored modal definitions) based on your neeed, you have to subclass arraycontroller because class name remains same (Entity) whether you need employee object or customer object.
我使用 NSArrayController 的子类来命名用于在核心数据应用程序中添加和删除对象的撤消/重做操作。
(这不是我自己的想法,功劳归于回答我对此事的问题。)
覆盖
- newObject
方法。还有
-remove:sender
方法。I use a subclass of NSArrayController to name the Undo/Redo actions for adding and removing objects in my Core Data application.
(It was not my own idea, the credit goes to user @MikeD who answered my question on this matter.)
Override the
- newObject
method.Also the
- remove:sender
method.