objective c - How to use a single NSValueTransformer subclass to toggle the titles of multiple menu items -
I would like to do some binding which would be toggled item titles in the popup menu based on a numeric value in a text field. Explain to me:
This is my UI:
< / P>
I want to automatically adjust my menu items for singular or plural based on the number in the text field. For example, when I type "1" in the box, I want to label the menu item "minute", "hour" and "day" When I type "4", I want to label the menu item "minute", "hour" and "day".
What did I do to date:
- I labeled three menu items in the same key path as the value of the text field Forced.
- I have created an NSValueTransformer subclass, which interprets the value in the text field and singular or plural
- I have implemented this value transformer in three bindings.
Problem:
My value transformer can return either singular or plural but it sets the appropriate string based on that menu item Can not do that it has been implemented.
It seems that the value transformer is normal and it is not different values for different destinations back, this will mean that the three titles will be converted into itself, I need to have three different price transformers Which will return the appropriate string for each menu item. It does not seem absolutely optimal.
Ideally, I can add the string stored in the Destination menu to destination menu item (Let's say in the tag property of the item), with the value of the text field is greater than one, or Something that enables me to use the same value transformer for all menu items
Am I missing something? What would be an ideal solution?
OK, this is probably not an ideal solution, but something might happen: if If you set your price transformer to your code (and not in IB), then you can instantize 3 different transformers of the same category. You can add something like your value transformer ivar NSString * unit
(and [[MyValueTransformer alloc] initWithUnit:]
, allowing each to return its string, but You still have to write the code of the price transformer only once.
(Also, if you are ever considering to localize your application, then just add "s" to create plurals. You are definitely not working both NSString * singular and
NSString * plural
.)
EDIT: Wait, I just realized that you can register the price transformer! If you give them MyValueTransformerHours
and MyValueTransformerMinutes
(manually alloc
ating and init
by ializing them in your code) As you register, you can do use them with the interface builder.
Comments
Post a Comment