How to subclass NSFormatter with NSPopUpButton

I want to use a subclass of NSFormatter to format the content of the NSPopUpButton used to display a list of options.

Basically I have an NSArray from MyObjects bound to an NSPopUpButton using a Content Values ​​binding and I want to display something in the popup menu other than what is returned by description. I really don't want to use a transformer because then I need to create a completely new array and convert each object to a string before adding it to the new array.

Using setFormatter: On the NSPopUpButton itself via IB or the code doesn't work, I suspect that only single cell formatting is applied to the items in the list.

Is there an easy way to set a formatter for all NSPopUpButton cells? Basically, I want to just install it once and forget about it.

0


source to share


1 answer


Typically you bind your popup button to an array controller that contains custom model objects (via content binding) and uses content value binding to provide the key path for that object with the string you want to use as the title.

From what I understand, you have an array of simple strings that you want to use as a data source, only you want to display another row for the title, right?



I'm not sure why creating a data transformer won't work if it's configured like above, or maybe I am missing something? You have one binding to an actual string and another binding to a string using a transformer, but both will use the same array controller. Your other options create a model class to wrap strings and provide a title property, or create a category in an NSString that returns your title to use as a display value binding. In all these cases, you can create your own NSFormatter in code and use it to return the correct row header.

+1


source







All Articles