理解和重现 KVC Hillegass 在控制器中插入/删除对象的方法

发布于 2024-12-21 12:04:14 字数 672 浏览 3 评论 0原文

在 Aaron Hillegass 的 Mac OS X 的 Cocoa 编程 中,Raiseman 应用程序连接Interface Builder (IB) 中的按钮到带有发送操作 -remove:NSArrayController。在 MyDocument 类中,他实现了两个 KVC 方法:

- (void)insertObject:(Person *)p inEmployeesAtIndex:(int)index;
- (void)removeObjectFromEmployeesAtIndex:(int)index;

按下此按钮时,将调用 -removeObjectFromEmployeesAtIndex: 方法,并从数组中删除当前选定的 Person(模型)对象。

  1. IB 中使用的 remove: 方法如何导致调用 -removeObjectFromEmployeesAtIndex: 方法?
  2. 如何使用 NSTreeController 重现这种效果?

In Aaron Hillegass' Cocoa Programming for Mac OS X, the Raiseman application connects a button in Interface Builder (IB) to an NSArrayController with sent action -remove:. In the MyDocument class he implements two KVC methods:

- (void)insertObject:(Person *)p inEmployeesAtIndex:(int)index;
- (void)removeObjectFromEmployeesAtIndex:(int)index;

When this button is pressed, the -removeObjectFromEmployeesAtIndex: method is called and the currently selected Person (Model) object is removed from the array.

  1. How does the remove: method used in IB cause the -removeObjectFromEmployeesAtIndex: method to be called?
  2. How do I reproduce this effect with an NSTreeController?

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

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

发布评论

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

评论(1

蒗幽 2024-12-28 12:04:14

如果您想要一个简单的内置选项,那么它只会创建您在 IB 中指定的类的实例。要创建另一个实例,您需要自己编写代码。您应该从树控制器中获得将新类插入到层次结构中正确位置所需的所有信息。一些勤奋的搜索应该会给你你需要的代码。

为了帮助您理解 NSArrayController 机制的工作原理,我将根据我对 Objective-C 和运行时的了解,尽我所能地进行解释。 Objective-C 是一种非常动态的语言,您可以动态调用选择器(方法)。由于 NSArrayController 知道您的类的名称(例如“Employee”),因此其内部实现可能类似于以下内容(或者很容易实现):

NSString *removeSelectorName = [NSString stringWithFormat:@"removeObjectFrom%@sAtIndex:",
                                self.objectClassName];
SEL removeSelector = NSSelectorFromString(removeSelectorName);

[dataRepresentation performSelector:removeSelector
                         withObject:[NSNumber numberWithInt:self.selectionIndex];

KVO 中的其他地方有这样的示例,例如+keyPathsForValuesAffecting 方法 (此处的文档) ,它描述了哪些键导致另一个键被更新。如果您的密钥名为 fullName 并且每当名字或姓氏更改时它就会更新,您可以在您的类中实现此功能:

+ (NSSet *)keyPathsForValuesAffectingFullName {
    return [NSSet setWithObjects:
            @"firstName",
            @"lastName",
            nil];
}

进一步搜索(以及 这个问题)出现了这个文档页面,它解释了如何调用该方法的语义。

If you want a simple built-in option, then it's only going to create an instance of the class you specified in IB. To create another instance, you're going to need to code it yourself. You should have all the information you need from the Tree Controller to insert the new class into the proper place in the hierarchy. Some diligent searching should give you the code you need.

To attempt to help you understand how the NSArrayController mechanism works, I'll explain the best I can from my knowledge of Objective-C and the runtime. Objective-C is a very dynamic language, and you can dynamically call selectors (methods). Since the NSArrayController knows the name of your class (e.g. "Employee"), its internal implementation probably looks something like the following (or easily could):

NSString *removeSelectorName = [NSString stringWithFormat:@"removeObjectFrom%@sAtIndex:",
                                self.objectClassName];
SEL removeSelector = NSSelectorFromString(removeSelectorName);

[dataRepresentation performSelector:removeSelector
                         withObject:[NSNumber numberWithInt:self.selectionIndex];

There are examples of this elsewhere in KVO, as with the +keyPathsForValuesAffecting<Key> method (documentation here), which describes which keys cause another key to be updated. If your key is named fullName and it updates whenever the first or last name changes, you would implement this in your class:

+ (NSSet *)keyPathsForValuesAffectingFullName {
    return [NSSet setWithObjects:
            @"firstName",
            @"lastName",
            nil];
}

Further searching (and this question) turned up this documentation page, which explains the semantics of how that method gets called.

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