如何使用NSFormatter子类与NSPopUpButton [英] How do I use an NSFormatter subclass with an NSPopUpButton

查看:174
本文介绍了如何使用NSFormatter子类与NSPopUpButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用NSFormatter子类来格式化NSPopUpButton的内容,我使用它来显示一个选择列表。

I want to use an NSFormatter subclass to format the contents of an NSPopUpButton I'm using to display a list of choices.

基本上我有一个NSArray的MyObjects它通过内容值绑定绑定到NSPopUpButton,并且我想在弹出菜单中显示除了-description返回的内容之外的东西。我真的不想使用一个变压器,因为我必须创建一个全新的数组,并将每个对象转换为字符串,然后再将其添加到新数组。

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.

使用setFormatter:在NSPopUpButton本身通过IB或代码不工作,我怀疑,因为只有单个单元格的格式化器应用于列表中的项目。

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.

有一个简单的方法来为NSPopUpButton的所有单元格设置格式化程序?

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.

推荐答案

通常,你将弹出按钮绑定到一个包含自定义模型对象(通过内容绑定)的数组控制器,并使用内容值绑定来指定这些对象上的键值,以及您要用作标题的字符串。

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?

我不知道为什么要做一个数据变压器不会工作,如果设置像上面 - 或者也许我误会了什么?你将有一个绑定到实际的字符串,并使用变压器的另一个绑定到字符串,但是他们都将使用相同的数组控制器。您的其他选项是创建一个模型类来包装字符串并提供一个title属性,或者在NSString上创建一个返回标题以用作显示值绑定的类别。在所有这些情况下,您可以在代码中创建自己的NSFormatter,并使用它返回正确的字符串标题。

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.

这篇关于如何使用NSFormatter子类与NSPopUpButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆