Hi I am using uislider and get the values in label. now i try to save the slider values. I don't know how to do this. If anybody knows please share the code. This is my code for slider changed.
-(IBAction)sliderChanged:(id)sender
{
UISlider *slider = (UISlider *)sender;
if((int)slider.value % 10 == 0)
{
sliderLabel.text = [[NSString alloc] initWithFormat:@"Value of:%d", (int)slider.value];
}
}
Asked
Active
Viewed 1,056 times
0

Jaikannan
- 57
- 11
-
what do you mean by 'saving', you mean storing that value so it's available when you restart the app? – JP Hribovsek Nov 10 '12 at 07:43
2 Answers
2
You can use NSUserDefault for storing the slider value.
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:sliderLabel.text forKey:@"Slider"];
[defaults synchronize];
You can retrieve the data like:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString *strValue = [defaults objectForKey:@"Slider"];
myLabel.text = strValue != nil ? strValue : @"No Value";
The above code is saving the sliderLabel
to the NSUserDefault
. If you want to store the slider value in the form of float then use:
[defaults setObject:[NSNumber numberWithFloat:slider.value] forKey:@"Slider"];
Retreive it like:
NSNumber *strValue = [defaults objectForKey:@"Slider"];

Midhun MP
- 103,496
- 31
- 153
- 200
-
-
@JPHribovsek: Yes, me too but he said like he want to display it in the UILabel, so it's a better Idea to store it as string :) – Midhun MP Nov 10 '12 at 07:51
-
yeah, but you know his next question will be about how to set the slider back to that value when reopening the app, then he will be stuck with an ugly conversion from string back to float – JP Hribovsek Nov 10 '12 at 07:54
0
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithFloat:slider.value] forKey:@"sliderValue"];
I believe you should store the value as a float (the original type for a UISlider value), it's just cleaner, and you'll probably want to reuse the float value to set back your slider when reopening the screen with something like:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[self.slider setValue:[[defaults objectForKey@"sliderValue"] floatValue]];

JP Hribovsek
- 6,707
- 2
- 21
- 26