如何将 NSFormatter 子类与 NSPopUpButton 一起使用
我想使用 NSFormatter 子类来格式化我用来显示选项列表的 NSPopUpButton 的内容。
基本上我有一个 MyObjects 的 NSArray,它通过内容值绑定绑定到 NSPopUpButton,并且我想在弹出菜单中显示除 -description 返回的内容之外的其他内容。 我真的不想使用转换器,因为这样我必须创建一个全新的数组并将每个对象转换为字符串,然后再将其添加到新数组中。
通过 IB 或代码在 NSPopUpButton 本身上使用 setFormatter: 不起作用,我怀疑是因为只有单个单元格的格式化程序才会应用于列表中的项目。
有没有一种简单的方法来为 NSPopUpButton 的所有单元格设置格式化程序? 基本上我希望只设置一次然后就可以忘记它。
I want to use an NSFormatter subclass to format the contents of an NSPopUpButton I'm using to display a list of choices.
Basically I have an NSArray of MyObjects which is bound to the NSPopUpButton via the Content Values binding and I want to display something in the pop up menu other than what is returned by -description. I don't really want to use a transformer because then I have to create an entirely new array and transform each object into a string before adding it to the new array.
Using setFormatter: on the NSPopUpButton itself via either IB or code doesn't work, I suspect because only the formatter for the individual cell is applied to the items in the list.
Is there an easy way to set a formatter for all the cells of the NSPopUpButton? Basically I want to just be able to set it once and forget about it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,您会将弹出按钮绑定到包含自定义模型对象的数组控制器(通过内容绑定),并使用内容值绑定来指定这些对象上的键路径以及要用作标题的字符串。
据我了解,您有一个纯字符串数组要用作数据源,只是您想为标题显示不同的字符串,对吗?
我不确定为什么如果像上面那样设置,那么制作数据转换器就行不通——或者也许我误解了什么? 您将有一个绑定到实际字符串,另一个绑定到使用转换器的字符串,但它们都将使用相同的数组控制器。 您的其他选择是创建一个模型类来包装字符串并提供标题属性,或者在 NSString 上创建一个类别,该类别返回您的标题以用作显示值绑定。 在所有这些情况下,您都可以在代码中创建自己的 NSFormatter 并使用它返回正确的字符串标题。
Typically you'd bind your popup button to an array controller that contains custom model objects (through the content binding), and use the content values binding to specify a keypath on those object with the string you want to use as the title.
From what I understand, you have an array of plain strings you want to use as the data source, only you want to display a different string for the title, right?
I'm not sure why making a data transformer wouldn't work if set up like the above-- or maybe I'm misunderstanding something? You'd have one binding to the actual string, and another binding to the string using a transformer, but both of them would be using the same array controller. Your other options are creating a model class to wrap around the strings and provide a title property, or creating a category on NSString that returns your title to use as the display value binding. In all of these cases you can create your own NSFormatter in code and use it to return the correct string title.