Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
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

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
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

macrumors 603
Jan 6, 2004
6,265
2,629
Western US
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

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
MrFusion said:
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

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
Value Transformers work both ways and you can do whatever you want in them.
Code:
+ (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
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.