子类化 NSArrayController 的原因是什么?

发布于 2024-10-30 03:36:43 字数 77 浏览 4 评论 0原文

我正在尝试改进我的 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 技术交流群。

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

发布评论

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

评论(5

甜扑 2024-11-06 03:36:43

我有一个自定义的 NSArrayController 子类,可以执行一大堆任务。我选择在那里实现这些东西是因为这样我就可以享受绑定和其他东西的全部舒适感。这就是我现在使用它的目的:

  • 有时某些项目必须隐藏,有些必须显示
  • 我在控制器中执行自定义排序(即分组)
  • 它由与返回的项目不同种类的项目提供(获取项目,返回项目节点) - 转发大多数内容的虚拟对象)
  • 我还使用它来保存当前显示的过滤条件和搜索选项
  • 此外,我添加了 NSTableView 委托和数据源支持,允许拖放将实现直接放在控制器中
  • 我还为其中的单元格自定义了工具提示

是的,等等。基本上,这一切都归结为这个本质:如果您想要输出的数据与输入的数据不同,则子类化 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:

  • Sometimes some items must be hidden and some must be shown
  • I perform custom sorting (i.e. grouping) in the controller
  • It is fed by the items of a different kind than it returns (get items, returns item nodes - dummy objects that forward most stuff)
  • I also use it to hold the filter criteria and search options currently on display
  • In addition, I added NSTableView delegate and data source support that allows drag & drop implementation right in the controller
  • I also customize tool tips for the cell in there

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 上发布了这个:

// Subclass of NSArrayController

- (void)awakeFromNib
{
    [[NSNotificationCenter defaultCenter] addObserver: self 
                                             selector: @selector(objectAdded:) 
                                                 name: @"Object Added" 
                                               object: self]
}

- (void)add: (id)sender
{
    [super add: sender]
    NSNotification * note = [NSNotification 
                                notificationWithName: @"Object Added" 
                                              object: self]
    // The add method doesn't really take effect until this run loop ends,
    // (see NSArrayController docs) so the notification needs 
    // to wait to post. Thus, enqueue with NSPostWhenIdle
    [[NSNotificationQueue defaultQueue] enqueueNotification: note
                                               postingStyle: NSPostWhenIdle]
}

- (void)objectAdded: (NSNotification *)note
{
    // when the notification finally arrives, tell the table to edit
    [[self contentTable] editColumn:0 
                                    row:[self selectionIndex] 
                              withEvent:nil 
                                 select:YES]
}

当然,可以对不是 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:

// Subclass of NSArrayController

- (void)awakeFromNib
{
    [[NSNotificationCenter defaultCenter] addObserver: self 
                                             selector: @selector(objectAdded:) 
                                                 name: @"Object Added" 
                                               object: self]
}

- (void)add: (id)sender
{
    [super add: sender]
    NSNotification * note = [NSNotification 
                                notificationWithName: @"Object Added" 
                                              object: self]
    // The add method doesn't really take effect until this run loop ends,
    // (see NSArrayController docs) so the notification needs 
    // to wait to post. Thus, enqueue with NSPostWhenIdle
    [[NSNotificationQueue defaultQueue] enqueueNotification: note
                                               postingStyle: NSPostWhenIdle]
}

- (void)objectAdded: (NSNotification *)note
{
    // when the notification finally arrives, tell the table to edit
    [[self contentTable] editColumn:0 
                                    row:[self selectionIndex] 
                              withEvent:nil 
                                 select:YES]
}

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.

怀中猫帐中妖 2024-11-06 03:36:43

我有一个应用程序需要在用户添加对象时设置隐藏文件名 - 自定义 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.

飘逸的'云 2024-11-06 03:36:43

我对数组控制器进行了子类化,以在调用 -(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.

街角卖回忆 2024-11-06 03:36:43

我使用 NSArrayController 的子类来命名用于在核心数据应用程序中添加和删除对象的撤消/重做操作。
(这不是我自己的想法,功劳归于回答我对此事的问题。)

覆盖 - newObject 方法。

- (id)newObject
{
    id newObj = [super newObject];

    NSUndoManager *undoManager = [[[NSApp delegate] window] undoManager];
    [undoManager setActionName:@"Add *insert custom name*"];

    return newObj;
}

还有 -remove:sender 方法。

- (void)remove:(id)sender
{
    [super remove:sender];

    NSUndoManager *undoManager = [[[NSApp delegate] window] undoManager];
    [undoManager setActionName:@"Remove *insert custom name*"];
}

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.

- (id)newObject
{
    id newObj = [super newObject];

    NSUndoManager *undoManager = [[[NSApp delegate] window] undoManager];
    [undoManager setActionName:@"Add *insert custom name*"];

    return newObj;
}

Also the - remove:sender method.

- (void)remove:(id)sender
{
    [super remove:sender];

    NSUndoManager *undoManager = [[[NSApp delegate] window] undoManager];
    [undoManager setActionName:@"Remove *insert custom name*"];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文