开发者

iPhone slider save user preference

开发者 https://www.devze.com 2023-01-30 07:17 出处:网络
I need to save a slider preference in my Settings.bundle. I\'m using the following code to write to the Settings.bundle:

I need to save a slider preference in my Settings.bundle.

I'm using the following code to write to the Settings.bundle:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:mySlider.value forKey:@"TimerSliderKey"];
[defaults synchronize];

And the following code to read from the Settings.bundle:

NSUserDefaults *defaults2 = [NSUserDefaults standardUserDefaul开发者_运维问答ts];
int valueForSlider = [defaults2 integerForKey:@"TimerSliderKey"];
NSLog(@"The saved slider value is %@" , valueForSlider);

This code does not work. Can someone give me some replacement code that I could use?


Your problem is here:

NSLog(@"The saved slider value is %@" , valueForSlider);

You need to use the %d format specifier, %@ is the format specifier to send the description message to the argument.

NSLog(@"The saved slider value is %d" , valueForSlider);

Here's a list of format specifiers:

iPhone slider save user preference

See the String Programming Guide for more information.


The other, and perhaps the real, problem is that UISlider's value ranges from 0.0 to 1.0 by default. Storing it as an integer, you will only ever see 0 or 1 due to truncation.

You should store it as a floating point instead to fully preserve the value of the slider.


use this one...

[**yourslider** addTarget:self action:@selector(sliderValueChanged:)
                           forControlEvents:UIControlEventValueChanged];

if ([[NSUserDefaults standardUserDefaults]floatForKey:@"value"]) {

    yourslider.value= [[NSUserDefaults standardUserDefaults]floatForKey:@"value"];

  }


**--save your value in this method**

-(IBAction)sliderValueChanged:(UISlider *)sender
{

    NSLog(@"slider value = %f", sender.value);


   [[NSUserDefaults standardUserDefaults]setFloat:sender.value forKey:@"value"];

}
0

精彩评论

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