-1

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];
}
Jan
  • 653
  • 1
  • 7
  • 22

2 Answers2

0

The problem is that what you are saving into NSUserDefaults is not the slider's value but some weird other value derived from the value (by doing some incomprehensible lookup on the numbers array). Just save the actual value, because that is the slider's position.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • No, when I log the value it has the correct value. Even when I set a fixed value it has a wrong position for example, slider.value = (float) 75000; will make the car stand on 100+ instead of 75 KM – Jan Dec 10 '15 at 16:36
  • Yes. You are changing the value with your `numbers` array. I'm saying, lose that logic (temporarily), in both places in your code, and you will see that just saving and restoring the actual float value directly works. Then you can debug your `numbers` stuff, which is the source of the problem. – matt Dec 10 '15 at 16:39
  • Yes, you are right and it is working fine, but that doesn't solve my problem as I want fixed values for the slider. – Jan Dec 10 '15 at 16:50
  • Right, but you're missing my point. :) I'm saying that _you_ are hitting yourself in the face and then complaining that your face is being hit. Your way of turning the actual value into a fixed interval step is flawed. You need to debug it. – matt Dec 10 '15 at 17:34
0

Got it, I stored the slider.value and the NUMBER value in two seperated NSUserdefaults.

- (void)valueChanged:(UISlider *)sender {

    NSUInteger index = (NSUInteger)(slider.value + 0.5);
    [slider setValue:index animated:NO];
    NSNumber *number = numbers[index];

    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:slider.value] forKey:@"totalValue"];    
    [[NSUserDefaults standardUserDefaults] setObject:number forKey:@"distValue"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

Thanks for pointing it out. Rg.

Jan
  • 653
  • 1
  • 7
  • 22