Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSArrayController: How to programmatically clear selection?

Very simple question that's driving me crazy: What's the proper way to clear the selection of an NSArrayController programmatically?

I'm designing a view with the following components:

  • NSArrayController *controller1: Bound to an array of objects
  • NSPopUpView view1: Content bound to controller1.arrangedObjects; Value bound to controller1.selection; "Inserts Null Placeholder" selected
  • NSArrayController *controller2: Bound to an array stored in controller1.selection
  • NSPopupView view2: Content bound to controller2.arrangedObjects; value bound to controller2.selection; "Inserts Null Placeholder" selected

Initially, view1's content is populated; controller1 and controller2 have nil selection values; and view1 and view2 display null placeholders. Selection of controller1 causes controller1's selection to change and view2's content to populate. All good.

I'd like to implement a Clear button that clears the selection of controller1, which, thanks to bindings, should also clear the selection of controller2 and reset view1 and view2 to the null placeholder. For the life of me, I can't figure out the proper code for this very simple function. Altering the selection of controller1 fails to update the value shown in view1. Worse, altering the controller1 selection programatically causes weird things to happen in controller2: further selection of values in view1 fails to have any effect on view2.

Things I've tried:

  • Calling the SetSelectedObjects method of controller1 with an [NSArray new].

  • Calling the SetSelectedObjects method of controller1 with null.

  • Calling the SetSelectedIndex method of controller1 with NSNotFound.

  • Calling the RemoveSelectedIndex method of controller1 with the SelectedIndex property of controller1.

  • Looking in the Cocoa NSArrayController documentation for any class method or suggestion for clearing the selection value. Nothing there - not even any mention of this being desirable, let alone how to accomplish it.

Any ideas? Thanks...

like image 285
David Stein Avatar asked Feb 20 '23 10:02

David Stein


1 Answers

According to Apples Developer documentation this can be done using setSelectionIndexes:

To deselect all indexes, pass an empty index set.

Objective-C:

[arrayController setSelectionIndexes:[NSIndexSet indexSet]];

Swift:

arrayController.setSelectionIndexes( NSIndexSet() )

like image 54
freytag Avatar answered Mar 15 '23 21:03

freytag