views:

90

answers:

1

This question is similar to this one: http://stackoverflow.com/questions/821363/how-do-i-use-an-nsformatter-subclass-with-an-nspopupbutton

As mentioned in that question, it seems like the 'formatter' used by the cell of a NSPopUpButton doesn't seem to work. I'm wondering if this is expected, or if there is actually a purpose to setting the formatter of a NSPopUpButton.

Right now, I have a NSPopUpButton whose "Content Objects" are bound to the arrangedObjects of a NSArrayController whose "Content Array" is a NSArray of NSNumbers. Setting the formatter of the NSPopUpButton cell to a simple NSNumberFormatter which formats NSNumbers in a currency format doesn't work; the pop up menu displays the numbers un-formatted.

I'm wondering how I can format strings displayed in the pop up menu of an NSPopUpButton? I feel like this should be fairly straight-forward; having to use a value transformer, or a special value for the display path, seems like overkill and should be easier.

Thanks in advance.

A: 

If the cell won't honor the formatter, then you could provide an alternative property like -formattedCost as opposed to -cost. Nothing fancy is needed since a popup button's menu items are not user-editable.

Your -formattedCost property would use a shared NSNumberFormatter instance and return the properly-formatted string of -cost.

- (NSString *)formattedCost
{
    return [mySharedCurrencyFormatter stringFromNumber:[self cost]];
}

The "formattedCost" property is what you'd bind to for display. Additional caveats: you'll want to register the "formattedCost" key as being dependent on the "cost" key. That way, when costs are changed, your popup will update (because that triggers "formattedCost" to change as well).

Joshua Nozzi
Thanks Joshua. Your suggestion will certainly work, but I feel like there should be an easier way to accomplish this. With your suggestion, I would no longer be able to have the content of the NSPopUpbutton be a simple NSArray of NSNumbers. As you mentioned, there are some additional caveats to worry about with your suggestion, which I would like to avoid and just have something very simple and easy to work with. It's very strange that the NSPopUpButton doesn't honor the formatter, after all what else could it be used for?
CJ