How do I store 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 the label and setting the time on the label using slider.now. Suppose I start a timer, so the label value decreases every minute, and the slider value also decreases. want that if i close my app and then reopen the timer it was already working + label value matches time + slider value matches time? here is a picture of what i am doing

alt text

+2


source to share


1 answer


I'm not sure if I'm missing something, but storing things in NSUserDefaults is very easy. To keep the slider value:

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

      

To store the label value:

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

      



To get them back, just undo it:

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

      

Personally, I would not store the string representation of the remaining time, just a float. Then you can restore the timer text using whatever existing code you use to convert the float value to string representation.

+8


source







All Articles