NSTableView 如何通过绑定修改 NSArrayController 的选择?

发布于 2024-08-18 09:09:22 字数 740 浏览 1 评论 0原文

假设我有以下情况:

替代文字

当我在 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 的选择。

Let's say I have the following situation:

alt text

When I select different rows in the NSTableView, it magically updates the NSArrayController (PersonController) selection. How is the NSTableView doing this? Does it do something like this:

- (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
 }

 //...
}

I ask because I'm implementing my own bindable NSControl, and I'd like it to modify the selection of the bound NSArrayController like an NSTableView would.

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

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

发布评论

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

评论(1

仅冇旳回忆 2024-08-25 09:09:22

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

Binding NSTableColumn["Last Name"].value to key path "arrangedObjects.lastName" of NSArrayController: 0x215fc0
Binding NSTable.content to key path "arrangedObjects" of NSArrayController: 0x215fc0
Binding NSTable.selectionIndexes to key path "selectionIndexes" of NSArrayController: 0x215fc0
Binding NSTable.sortDescriptors to key path "sortDescriptors" of NSArrayController: 0x215fc0
Binding NSTableColumn["First Name"].value to key path "arrangedObjects.firstName" of NSArrayController: 0x215fc0

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

这在 < a href="http://developer.apple.com/library/mac/documentation/Cocoa/Reference/CocoaBindingsRef/BindingsText/NSTableView.html#//apple_ref/doc/uid/NSTableView-DontLinkElementID_623" rel="nofollow noreferrer" >NSTableView 绑定参考,适用于 selectionIndexessortDescriptors

On a hunch, I overrode bind:toObject:withKeyPath:options: on the NSTableView and both the NSTableColumn objects and made them log their bindings. This is the output:

Binding NSTableColumn["Last Name"].value to key path "arrangedObjects.lastName" of NSArrayController: 0x215fc0
Binding NSTable.content to key path "arrangedObjects" of NSArrayController: 0x215fc0
Binding NSTable.selectionIndexes to key path "selectionIndexes" of NSArrayController: 0x215fc0
Binding NSTable.sortDescriptors to key path "sortDescriptors" of NSArrayController: 0x215fc0
Binding NSTableColumn["First Name"].value to key path "arrangedObjects.firstName" of NSArrayController: 0x215fc0

Even though the only bindings I made were to "value" of the NSTableColumn objects, it looks like IB is adding additional bindings automatically. NSTableView is able to modify the NSArrayController selection because it's binding selectionIndexes automatically in IB.

This is confirmed in the NSTableView Bindings Reference, for both selectionIndexes and sortDescriptors.

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