Search code examples
objective-cxcodeiphone-sdk-3.0nsuserdefaultsuislider

how to save slidervalue and label value in nsuserdefaults or any other efficient way?


i have a view with one slider and a label.i am showing a countdown on label and setting time on label using slider.now suppose i have started the timer so the label's value is decreasing every minute and slider's value is also decreasing.now i want that if i close my app and then reopen the timer should be already running+label's value is according to time+slider value is according to time? here is an image what i am doing

alt text


Solution

  • I'm not sure if I'm missing something, but storing stuff in NSUserDefaults is super easy. To save the slider's value:

    [[NSUserDefaults standardUserDefaults] setFloat:[mySlider value] forKey:@"sliderValue"];
    

    To save the label's value:

    [[NSUserDefaults standardUserDefaults] setValue:[myLabel text] forKey:@"textValue"];
    

    To get them back, simply reverse it:

    [mySlider setValue:[[NSUserDefaults standardUserDefaults] floatForKey:@"sliderValue"]];
    

    Personally, I wouldn't save the string representation of the time left, just the float. You can then restore the timer's text using whatever existing code you're using to convert the float value to a string representation.