PDA

View Full Version : valuetransformers, bindings




MrFusion
Jul 5, 2006, 09:06 AM
I have a table with a column of values (let's say distances) and another column (with popupbuttoncells) that allows you to choose the unit (miles, meters, kilometers,...).
How can I force the value to update when the user selects another unit (I tried looking for some notification) and how can I let the valuetransformer find out which unit is selected?

Note that in all this I don't want to change the original value stored in the database whose unit is set to a default unit.

I could make an array with al the values precalculated, but if there are a lot and the user never changes the default unit, then it's a lot of wasted time waiting for the data to load.

Thanks



caveman_uk
Jul 5, 2006, 12:48 PM
I think you have to do a fake 'willChangeValueForKey:' 'didChangeValueForKey:' pair so the bindings system updates the values shown on screen.

I've used value transformers when I store a metric value internally but wish to display the value in the users choice of unit on screen. I stored the choice in a user default and used that in the value transformer to convert the value (both ways) and correctly format the display with an appropriate number of decimal places.

HiRez
Jul 5, 2006, 02:58 PM
No could make a custom NSNumberFormatter and attach it to the value column, which would convert the value to whatever unit the user has selected in the other column. There are probably several ways of doing it though.

MrFusion
Jul 6, 2006, 03:35 AM
I have a table with a column of values (let's say distances) and another column (with popupbuttoncells) that allows you to choose the unit (miles, meters, kilometers,...).
How can I force the value to update when the user selects another unit (I tried looking for some notification) and how can I let the valuetransformer find out which unit is selected?

Note that in all this I don't want to change the original value stored in the database whose unit is set to a default unit.

I could make an array with al the values precalculated, but if there are a lot and the user never changes the default unit, then it's a lot of wasted time waiting for the data to load.

Thanks


Thanks for the answers so far. I forgot to mention another difficulty, that is, when the user enters a value in one portion of the program the user can also select a unit for this. In another portion, the user can select a unit so that values can be compared to each other. But when doing so, the underlying value and unit in the database can not change.


I thought numberformatters were only for display characteristics and not for calculations.

caveman_uk
Jul 6, 2006, 04:41 AM
Value Transformers work both ways and you can do whatever you want in them.

+ (Class)transformedValueClass
+ (BOOL)allowsReverseTransformation
- (id)transformedValue:(id)item - can used to convert internal values to those displayed on screen
- (id)reverseTransformedValue:(id)item - can be used to convert inputed numbers to an internal format

As I said, in my program I store values as metric, for example the weights are stored as grammes. The user can chose the display entry units as g,kg,oz or lbs. They can also enter the values in their chosen units. The value Transformer converts the values to and from the stored value in grammes

Check out the links at the bottom of this page (http://www.cocoadev.com/index.pl?NSValueTransformer)

gekko513
Jul 6, 2006, 05:10 AM
Suggestion: Bind the popupbutton to an NSArrayController for an array, then make a manual binding between the selected or the selectedIndex property of the arraycontroller and a key-value-compliant method pair in the valuetransformer.

The implementation of the method pair should then let you know when the user selects something different.