0

I'm having a problem with a custom NSPopUpButtonCell in a table that's instantiated when the table view is populated via bindings and a NSArrayController.

The pop up button cell is created but when attempting to access the outlet by overriding the pop up button cell's setMenuItem:item method it's nil.
Is this the expected behaviour..?
Should another method be used to replace the menu at creation time?

Basically I need the outlet to link back to my controller (NSWindowController) for that document window so I can customize the NSPopUpButtonCell menu accordingly from the custom popup button when it's populated.

A solution using bindings would be even better - but when overriding setObjectValue: I can see it's only never called with a nil parameter.. using a stock NSPopUpButtonCell results in a properly populated pop up menu, though.
(see also Why is NSPopUpButtonCell showing correctly when only setObjectValue:nil is called).

Community
  • 1
  • 1
Jay
  • 6,572
  • 3
  • 37
  • 65
  • Is your popup button's menu also populated by bindings? If so, then you don't update it through an outlet, but by updating the array to which it is bound. – rdelmar Nov 19 '12 at 20:03
  • It is - but `setObjectValue:` is never called with valid content, it's always `nil` (see also [Why is NSPopUpButtonCell showing correctly when only setObjectValue:nil is called](http://stackoverflow.com/questions/9398496/why-is-nspopupbuttoncell-showing-correctly-when-only-setobjectvaluenil-is-calle)). So how to get to the bound values from within the `NSPopUpButtonCell`..? – Jay Nov 19 '12 at 21:09
  • Maybe I'm not understanding your situation -- I don't really know what you mean by getting the values from within the popup button cell. Those values come from an array, so you should get the values from there. IF you can give a little more detail about what you're trying to do, it would be helpful. – rdelmar Nov 20 '12 at 00:08
  • Question rephrased - I suppose the question is how to populate the pop up button cell using bindings, i.e. what do we need to override. – Jay Nov 20 '12 at 06:22

1 Answers1

1

You don't need to override anything to populate an NSPopUpButtonCell in an NSTableView column. The thing to know is that you set the bindings on the NSTableColumn and not on the cell itself. Typically, you would have an NSArrayController in your xib that is bound to an NSArray containing all the options for the pop-up, and then you would select the column with the pop-up cell and go to it's bindings. Like in this screenshot (note the populated Content, Content Objects, and Selected Object bindings in the inspector on the right):

Screenshot of Xcode showing NSTableColumn bindings for pop-up cell column

If you want a working example, you can check out this project I whipped up for another StackOverflow question. There's a bunch of unrelated stuff pertaining to making the NSPopUpButtonCell use NSAttributedStrings, but the bindings in the xib constitute a working example of how to bind an NSTableColumn with a pop-up whose options are populated by bindings.

ipmcc
  • 29,581
  • 5
  • 84
  • 147
  • Thanks for the sample. However I was specifically looking for a way to **access** the values that are bound to the `NSPopUpButton` from its subclass and do something with them. – Jay Dec 04 '12 at 16:15
  • How to do that depends on what you mean by values. If you mean the options that appear in the menu when it's popped-up the easiest way is probably to make something the menu's delegate and implement the NSMenuDelegate method `menuNeedsUpdate:`. If you need the value that gets bound for what is currently selected, that's a bit hairier, but overriding `-[NSPopUpButtonCell setMenuItem:] will give you a chance to intercept the subordinate menu item that the cell will use to render its non-popped up state. – ipmcc Dec 04 '12 at 18:38
  • Right - I guess `menuNeedsUpdate:` is good enough for what I'm trying to achieve! It just struck me as odd that I couldn't just use override `setObjectValue:` and get all objects the array controller would *"feed"* into the popup menu.. – Jay Dec 05 '12 at 06:52
  • Yeah, Cocoa Bindings don't always work the way one might think they would. Sadly, the source isn't public, and the documentation isn't written to the level of detail that would be necessary to understand details like these. This tends to leave us with "tedious trial & error experimentation" as the primary recourse. – ipmcc Dec 05 '12 at 11:59
  • Totally - the level of documentation on Bindings is just *scary* for a pillar of modern Cocoa architecture – Jay Dec 05 '12 at 12:18