4

假设我有以下情况:

替代文字

当我在 NSTableView 中选择不同的行时,它会神奇地更新 NSArrayController (PersonController) 选择。NSTableView 是如何做到这一点的?它会做这样的事情吗:

- (void)bind:(NSString *)binding toObject:(id)observableController withKeyPath:(NSString *)keyPath options:(NSDictionary *)options;
{
 if([observableController isKindOfClass:[NSArrayController class]]){
  // got the NSArrayController, which can be used to change selection
 } else {
  // not an NSArrayController, don't try to change selection
 }

 //...
}

我问是因为我正在实现自己的可绑定 NSControl,并且我希望它能够像 NSTableView 一样修改绑定的 NSArrayController 的选择。

4

1 回答 1

9

凭直觉,我覆盖bind:toObject:withKeyPath:options:NSTableView这两个NSTableColumn对象,并让它们记录了它们的绑定。这是输出:

将 NSTableColumn["Last Name"].value 绑定到 NSArrayController 的键路径 "arrangedObjects.lastName": 0x215fc0
将 NSTable.content 绑定到 NSArrayController 的关键路径“arrangedObjects”:0x215fc0
将 NSTable.selectionIndexes 绑定到 NSArrayController 的关键路径“selectionIndexes”:0x215fc0
将 NSTable.sortDescriptors 绑定到 NSArrayController 的键路径“sortDescriptors”:0x215fc0
将 NSTableColumn["First Name"].value 绑定到 NSArrayController 的键路径 "arrangedObjects.firstName": 0x215fc0

尽管我所做的唯一绑定是对 NSTableColumn 对象的“值”,但看起来 IB 正在自动添加其他绑定。NSTableView能够修改NSArrayController选择,因为它selectionIndexes在 IB 中自动绑定。

这在NSTableView 绑定参考中得到了证实,对于selectionIndexessortDescriptors

于 2010-01-19T02:31:43.713 回答