NSFormatter 可以与 NSPopUpButton 一起使用吗?
这个问题与这个问题类似:我如何使用带有 NSPopUpButton 的 NSFormatter 子类
正如该问题中提到的, NSPopUpButton 的单元格使用的“格式化程序”似乎不起作用。我想知道这是否是预期的,或者设置 NSPopUpButton 的格式化程序是否确实有目的。
现在,我有一个 NSPopUpButton,其“内容对象”绑定到 NSArrayController 的排列对象,其“内容数组”是 NSNumbers 的 NSArray。将 NSPopUpButton 单元格的格式化程序设置为简单的 NSNumberFormatter(以货币格式格式化 NSNumbers)不起作用;弹出菜单显示未格式化的数字。
我想知道如何格式化 NSPopUpButton 弹出菜单中显示的字符串?我觉得这应该是相当简单的;必须使用值转换器或显示路径的特殊值似乎有点矫枉过正,应该更容易。
提前致谢。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果单元格不支持格式化程序,那么您可以提供替代属性,例如 -formattedCost,而不是 -cost。不需要任何花哨的东西,因为弹出按钮的菜单项是用户不可编辑的。
您的 -formattedCost 属性将使用共享的 NSNumberFormatter 实例并返回格式正确的 -cost 字符串。
“formattedCost”属性是您要绑定以进行显示的属性。其他注意事项:您需要将“formattedCost”键注册为依赖于“cost”键。这样,当成本发生变化时,您的弹出窗口将会更新(因为这也会触发“formattedCost”发生变化)。
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.
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).