views:

31

answers:

1

I am looking for a way to access user settings (I assume, NSUserDefaults?) through a button action. Let me back up and explain. What I have right now are 2 TextFields a label and a button. The user will type in measurements in the 2 TextFields. When they hit the button the label displays the volume of the measured object in Gallons. That part of it works great.

Then I wanted to give the user options to output the volume in Liters instead of gallons. I would also like to give the user options to type in the measurements in Centimeters. So I setup a 'Settings.Bundle' and configured it with 2 'Multi Value' cells (Measurement units and Volumetric Units). Each Multi Value cell has its own list of different units the user can pick from.

My main issue is I don't know how to access these settings through the button action. I may be thinking of this wrong, but what I'm looking for is something like;

Button Action
    If settings key = 0  
     Then do the math in Inches, Display in Gallons  
    If settings key = 1  
     Then do the math in Centimeters, Display in Gallons  
    If settings key = 2  
     Then do the math in Inches, Display in Liters  
    If settings key = 3  
     Then do the math in Centimeters, Display in Liters  
Etc...  

Is this possible? Am I thinking of this in the wrong way? What's the best way to do this?

+1  A: 

In your model you should store the values in one type of measurement unit. You don't "do the math" differently depending on what unit is being used, the calculations are always the same. You just need to accept and display units of different types.

In order to display and accept the values, your text fields should use an NSValueTransformer. A subclass of NSValueTransformer allows you to display a transformed version of a value and optionally, to reverse transform the value on input. So if your model stores values in centimetres, you'd use an NSValueTransformer that converts centimetres to inches and in its -reverseTransformValue: method, converts inches to centimetres. So, when the user types a value in inches, the value is stored in your model in centimetres.

In your case, your NSValueTransformer would need to use the current units setting that has been stored in NSUserDefaults to change the way it transforms the units.

Rob Keniger