4

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.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Tom Dalling
  • 23,305
  • 6
  • 62
  • 80

1 Answers1

9

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.

Dov
  • 15,530
  • 13
  • 76
  • 177
Tom Dalling
  • 23,305
  • 6
  • 62
  • 80
  • Thanks so much for this detective work! Working up the steam to write those overrides would have been painful. Your work helped me a lot, I don't know why somebody rated the answer as less useful. – Dennis Nov 24 '10 at 00:05