How do I Create a Default Slider Value Programmatically?

706 views Asked by At

The "current value" default I have set in interface builder for my sliders seems to be bypassed completely, and when I run the app, the sliders start from where I last had them set the last time I ran the app.

I would like the sliders to start from their default value AND recall the position they were last in, before switching to a different view.

Here is the code that allows the slider values to be saved:

viewController.m

- (void)viewWillAppear:(BOOL)animated{
    [_firstSlider setValue:[[NSUserDefaults standardUserDefaults] floatForKey:@"firstSliderValue"]];
}

- (IBAction)firstSliderValueChange:(id)sender {
    [[NSUserDefaults standardUserDefaults] setFloat:[_firstSlider value] forKey:@"firstSliderValue"];
}

Thank You!

4

There are 4 answers

4
TheJack On

As Dave Batton said, NSUserDefaults is for defaults on fresh launches of the app. If I understand your question correctly, it appears to me that you are overwriting your default value in firstSliderValueChange. That is why this happens:

the sliders start from where I last had them set the last time I ran the app.

Either store your defaults and in-app values separately or pass them between views.

EDIT: The code mentioned in your comment below doesn't pass values. It persists values.

Problem: Say you originally have set a slider value default of 0. When you run the app for the first time, you see the slider at 0. Now you move the slider to, say, 5. Then this method is called:

- (IBAction)firstSliderValueChange:(id)sender {
    [[NSUserDefaults standardUserDefaults] setFloat:[_firstSlider value] forKey:@"firstSliderValue"];
}

Now the default value is no longer 0. It is 5. And that persists between app launches, since you are using NSUserDefaults.

Solution: Save the dynamic slider value some place else - not in NSUserDefaults. Since you have multiple view controllers, one way is this:

//viewController.m
@interface
@property (strong, nonatomic) IBOutlet UISlider *mySlider;
@end

-(void)prepareForSegue: (UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString: @"segueIdentifer"]) {
    otherViewController *ovc = (otherViewController *)segue.destinationViewController;
    ovc.dynamicSliderValue = self.mySlider.value;
    }
}

And:

//otherViewController.h
@interface
@property(assign) CGFloat dynamicSliderValue;
@end

Now, whenever you switch back to your first view controller's view, simply access this property, and update the slider accordingly. Note - I am sure there are many other ways of doing this!

2
Dave Batton On

Don't use NSUserDefaults to store the value between views. That's not what it's for. Just pass the slider value from one view to another. You may need to do this in -prepareForSegue:sender:.

Use NSUserDefaults when you want to save and restore a value between fresh launches of the app.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ShowAnotherSlider"]) {
        DBAnotherViewController *destinationViewController = (DBAnotherViewController *)segue.destinationViewController;
        destinationViewController.sliderStartValue = self.slider.value;
    }
}

Notice that I'm setting a property on the destination view controller, not setting the slider value directly.

@property (nonatomic, strong) CGFloat sliderStartValue;

The destination view controller's view probably isn't loaded at this point, so the slider property is nil. Set this other property instead, and then when the view gets loaded (-viewDidLoad) you can update the slider with that value.

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.slider.value = self.sliderStartValue;
}
3
psobko On

Just remove the object for the key when the app is launching again:

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"firstSliderValue"];

You can do this in the AppDelegate or wherever else makes sense.

0
ajay_nasa On

Just add this [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"YourKey"]; as @psobko said in AppDelegate like:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"YourKey"];
    return YES;
}