如何使用单个 NSValueTransformer 子类切换多个菜单项的标题
我想进行一些绑定,根据文本字段中的单个数值在弹出菜单中切换项目标题。让我解释一下:
这是我的 UI:
我希望菜单项根据文本字段中的数字自动调整为单数或复数。例如,当我在框中输入“1”时,我希望菜单项标记为“分钟”、“小时”和“天”。当我输入“4”时,我希望菜单项标记为“分钟”、“小时”和“天”。
我迄今为止所做的事情:
- 我将三个菜单项的标签绑定到与文本字段的值相同的键路径。
- 我创建了一个 NSValueTransformer 子类来解释文本字段中的值并返回单数或复数用作项目标题。
- 我将此值转换器应用于三个绑定。
问题:
我的值转换器可以返回单数或复数,但它无法根据其应用的菜单项设置适当的字符串。
在我看来,值转换器是通用的,不能为不同的目的地返回不同的值。这意味着要自动更改三个标题,我需要三个不同的值转换器,为每个菜单项返回适当的字符串。这似乎根本不是最佳的。
理想情况下,当文本字段的值大于一时,我能够将存储在目标菜单项(假设在项目的标签属性中)中的字符串与值转换器中的“s”组合起来,或者类似的东西可以让我对所有菜单项使用单值转换器。
有什么我错过的吗?什么是理想的解决方案?
I would like to make some bindings that toggle item titles in a popup menu based on a single numerical value in a text field. Let me explain:
This is my UI:
I want my menu items to automatically adjust to singular or plural based on the number in the text field. For example, when I type "1" in the box, I want the menu items to be labeled "minute", "hour" and "day". When I type "4", I want the menu items to be labeled "minutes", "hours" and "days".
What I did to date:
- I bound the three menu item's labels to the same key path as the text field's value.
- I created an NSValueTransformer subclass to interpret the value in the text field and return singular or plural to be used as item titles.
- I applied this value transformer to the three bindings.
The issue:
My value transformer can either return singular or plural but it can't set the appropriate string based on the menu item it's applied to.
It looks to me like a value transformer is generic and can't return different values for different destinations. That would mean that to have the three titles change automatically, I would need to have three different value transformers that return the appropriate string for each menu item. That doesn't seem optimal at all.
Ideally I would be able to combine a string stored in the destination menu item (let's say in the item's tag property) with an "s" in the value transformer when the text field's value is larger than one, or something similar that would enable me to use a single value transformer for all the menu items.
Is there something I missed? What would be an ideal solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,这可能不是一个理想的解决方案,但您可以考虑一下:如果您从代码(而不是在 IB 中)设置值转换器,则可以实例化同一类的 3 个不同的转换器。您可以为您的值转换器提供一个 ivar
NSString *unit
(并添加类似[[MyValueTransformer alloc] initWithUnit:]
的内容)以允许每个值转换器返回自己的字符串,但是您仍然只需编写一次值转换器的代码。(此外,如果您打算考虑使您的应用程序可本地化,那么简单地附加一个“s”来创建复数是行不通的。您当然可以为
NSString *singular
添加 ivars和NSString *plural
来做到这一点。)编辑:等等,我刚刚意识到你可以注册值转换器!如果您将它们注册为
MyValueTransformerHours
和MyValueTransformerMinutes
(通过在代码中手动分配
和初始化
它们),您可以从 Interface Builder 使用它们。另请参阅 https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ValueTransformers/Concepts/Registration.html#//apple_ref/doc/uid/20002166-BAJEAIEE。Okay, this is probably not an ideal solution, but something you could consider: if you set your value transformers from your code (and not in IB), you could instantiate 3 different transformers of the same class. You could give your value transformer an ivar
NSString *unit
(and add something like[[MyValueTransformer alloc] initWithUnit:]
) to allow each to return their own string, but you still have to write the value transformer's code only once.(Also, if you're ever going to consider making your application localizable, simply appending an "s" to create the plurals is not going to work. You could of course add ivars for both
NSString *singular
andNSString *plural
to do that.)Edit: Wait, I just realized you can register value transformers! If you register them as
MyValueTransformerHours
andMyValueTransformerMinutes
(by manuallyalloc
ating andinit
ializing them in your code), you can use them from Interface Builder. See also https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ValueTransformers/Concepts/Registration.html#//apple_ref/doc/uid/20002166-BAJEAIEE.