开发者

Get settings through a button action

开发者 https://www.devze.com 2022-12-29 19:14 出处:网络
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 wi

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 Li开发者_如何学运维ters  
    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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消