开发者

Can a NSFormatter be used with a NSPopUpButton?

开发者 https://www.devze.com 2022-12-15 20:17 出处:网络
This question is similar to this one: 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 doe

This question is similar to this one: 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 NS开发者_StackOverflow中文版Numbers 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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消