Saving the value of UISlider
I've managed to implement a UISlider
in Cocos2D which actually works to my surprise.
I've been looking through all the NSUserDefaults
samples but I can't get anything to work correctly. Just wondered if anyone could help. I think I have the saving of the value correct.
- (void) valueChanged:(float) value tag:(int) tag{
if (tag == 1) // music volume
[self updateLabel:value];
[CDAudioManager sharedManager].backgroundMusic.volume = value;
CCLOG (@"Unknown slider");
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setFloat:value forKey:@"floatKey"];
[prefs synchronize];
}
It's just reloading the defaults where I'm kind of stuck, I've put this in the initialization of my class. I'm not even sure if it should go there...
NSUserDefaults *userDefaults = [NSU开发者_如何学运维serDefaults standardUserDefaults];
if ([userDefaults floatForKey:@"floatKey"]) {
[userDefaults setFloat:value forKey:@"floatKey"];
}
In your initialization, you are setting the value for floatKey
in the prefs instead of getting it. You probably want something like this:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
float value = [userDefaults floatForKey:@"floatKey"]; //read the value from the defaults
[CDAudioManager sharedManager].backgroundMusic.volume = value; //use the value
精彩评论