In my project im having a UISlider which has fixed positions which is working fine, but when I try to save the value using NSUserDefaults the position doesn't make any sense no more and it overwrites the fixed positions. I tried to devide the saved value with 5000 which is then coming close to the position but it still isn't the same position as the slider has by defaults.
What is the logical calculation for saving the exact same position?
NSString *afsValue = [[NSUserDefaults standardUserDefaults]
stringForKey:@"distValue"];
slider = [[UISlider alloc] initWithFrame:CGRectMake(28,205,264,50)];
[slider setThumbImage:[UIImage imageNamed:@"car80"] forState:UIControlStateNormal];
[slider setMinimumTrackTintColor:[UIColor colorWithRed:0.110f green:0.224f blue:0.400f alpha:0.85f]];
[slider setMaximumTrackTintColor:[UIColor whiteColor]];
[self.view addSubview:slider];
numbers = @[@(3000), @(5000), @(10000), @(15000), @(25000), @(50000), @(75000), @(500000)];
NSInteger numberOfSteps = ((float)[numbers count] - 1);
slider.maximumValue = numberOfSteps;
slider.minimumValue = 0;
slider.value = (float) [afsValue intValue]/5000;
slider.continuous = YES;
[slider addTarget:self
action:@selector(valueChanged:)
forControlEvents:UIControlEventValueChanged];
- (void)valueChanged:(UISlider *)sender {
NSUInteger index = (NSUInteger)(slider.value + 0.5);
[slider setValue:index animated:NO];
NSNumber *number = numbers[index];
[[NSUserDefaults standardUserDefaults] setObject:number forKey:@"distValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
}