How do I save a UISlider value?
How could I save the value of a UISlider ?I tried this, but it didn't work:
-(IBAction)save {
slider1i = slider1.value;
NSUserDefaults *slider1save = [NSUserDefaults standardUserDefaults];
[slider1save setInteger:slider1i forKey:@"slid开发者_开发问答er1key"];
[slider1save synchronize];
}
slider1i
is a NSinteger, and slider1
is the UISlider ...
Can you help-me ?
As user 698952 said, UISlider's value property is a float. But i'd instead use setFloat like this:
slider1i = slider1.value;
NSUserDefaults *slider1save = [NSUserDefaults standardUserDefaults];
[slider1save setFloat:slider1i forKey:@"slider1key"];
[slider1save synchronize];
slider1.value
is float
value so you first need to convert it in NSString or NSNumber inorder to save it ..... that makes it a object
-(IBAction)save {
NSNumber *sliderValue = [NSNumber numberWithFloat:slider1.value];
NSUserDefaults *slider1save = [NSUserDefaults standardUserDefaults];
[slider1save setObject:sliderValue forKey:@"slider1key"];
[slider1save synchronize];
}
thanks
精彩评论