2

I have a NSPopUpButton. Whenever I add or remove items to it, the first item gets automatically selected. I want to inhibit this; I want the NSPopUpButton to appear with nothing selected, and to not select anything itself until the user selects something.

Everything is being constructed in code. (This is a backend; the GUI layout comes from elsewhere.)

NSComboBox does this already.

With NSTableView, I used an NSArrayController and Cocoa bindings, so I could just do:

[ac setSelectsInsertedObjects:NO];              // for insertions
[ac setAvoidsEmptySelection:NO];                // for deletions

I tried this with NSPopUpButton, however, and that did not change the behavior: the first item is still automatically selected.

I have also tried accessing the backing NSMenu at someone on IRC's suggestion and adding to that; the behavior did not change.

I noticed there's a method on NSPopUpButton, synchronizeTitleAndSelectedItem, that seems to do exactly what I said NSPopUpButton does itself, but I don't see a way to disable it, either in NSPopUpButton or NSPopUpButtonCell. Am I going to have to subclass NSPopUpButton to make this method do nothing? I'm not particularly attracted to that idea.

Is there something else I'm doing wrong? Thanks.

andlabs
  • 11,290
  • 1
  • 31
  • 52

1 Answers1

0

Speculation, but a possible answer:

If you are using an arrayController, and you have bound the content but not the selectionIndex, the popup is going to keep selection index zero, even if the object at that index gets bumped elsewhere.

So bind the selection index to the array controller as well.

stevesliva
  • 5,351
  • 1
  • 16
  • 39
  • I can't seem to do that...? NSPopUpButton has an integer-type binding for selected index; NSArrayController has a NSIndexSet-type binding... unless there's something I'm missing that I don't know about – andlabs May 19 '14 at 04:04
  • Sorry, it was speculative, but if you *don't* have the selection bound, we might be on to something... try binding the popup's selected object to the AC's selection. Knowing that this might be something you haven't tried, I fired up IB and this might help. I take no offense if this is a dead end, though. – stevesliva May 19 '14 at 04:31
  • 1
    Tried it, but with `selectionIndex` (not `selectionIndices`) instead of `selectionObject`. THAT seems to do it, but I need to double-check (will mark answer once I do). Though... `selectionIndex` isn't in https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CocoaBindingsRef/BindingsText/NSArrayController.html but is in https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaBindings/Articles/ControllerKey-ValueObservingCompliance.html#//apple_ref/doc/uid/TP40002493-DontLinkElementID_3 - am I missing something else (such as a link between the two)? Thanks. – andlabs May 29 '14 at 03:44
  • Interesting. The more extensive list is *KVO* keys that you could bind to. So an AC doesn't expose a selectionIndex binding, but you can bind to it, I believe. – stevesliva May 29 '14 at 03:50
  • Ah, I see. Anyway it does work with the singular key path, so thanks again! (Sorry for the delay in my previous response.) – andlabs May 29 '14 at 08:20