NSTableView 如何通过绑定修改 NSArrayController 的选择?
假设我有以下情况:
当我在 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
凭直觉,我覆盖了
NSTableView
和两个NSTableColumn
对象上的bind:toObject:withKeyPath:options:
并让它们记录其绑定。这是输出:尽管我所做的唯一绑定是 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 绑定参考,适用于
selectionIndexes
和sortDescriptors
。On a hunch, I overrode
bind:toObject:withKeyPath:options:
on theNSTableView
and both theNSTableColumn
objects and made them log their bindings. This is the output: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 theNSArrayController
selection because it's bindingselectionIndexes
automatically in IB.This is confirmed in the NSTableView Bindings Reference, for both
selectionIndexes
andsortDescriptors
.