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];
}
}
问问题
1047 次
2 回答
2
您可以使用NSUserDefault来存储滑块值。
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:sliderLabel.text forKey:@"Slider"];
[defaults synchronize];
您可以检索数据,例如:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString *strValue = [defaults objectForKey:@"Slider"];
myLabel.text = strValue != nil ? strValue : @"No Value";
上面的代码是保存sliderLabel
到NSUserDefault
. 如果您想以浮点数的形式存储滑块值,请使用:
[defaults setObject:[NSNumber numberWithFloat:slider.value] forKey:@"Slider"];
像这样检索它:
NSNumber *strValue = [defaults objectForKey:@"Slider"];
于 2012-11-10T07:44:41.553 回答
0
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithFloat:slider.value] forKey:@"sliderValue"];
我相信您应该将值存储为浮点数(UISlider 值的原始类型),它更简洁,并且您可能希望在重新打开屏幕时重新使用浮点值来设置滑块,例如:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[self.slider setValue:[[defaults objectForKey@"sliderValue"] floatValue]];
于 2012-11-10T07:45:44.950 回答