how to store slider values in ios - ios

Hi I am using uislider and get the values in label. now i try to save the slider values. I don't know how to do this. If anybody knows please share the code. This is my code for slider changed.
-(IBAction)sliderChanged:(id)sender
{
UISlider *slider = (UISlider *)sender;
if((int)slider.value % 10 == 0)
{
sliderLabel.text = [[NSString alloc] initWithFormat:#"Value of:%d", (int)slider.value];
}
}

You can use NSUserDefault for storing the slider value.
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:sliderLabel.text forKey:#"Slider"];
[defaults synchronize];
You can retrieve the data like:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString *strValue = [defaults objectForKey:#"Slider"];
myLabel.text = strValue != nil ? strValue : #"No Value";
The above code is saving the sliderLabel to the NSUserDefault. If you want to store the slider value in the form of float then use:
[defaults setObject:[NSNumber numberWithFloat:slider.value] forKey:#"Slider"];
Retreive it like:
NSNumber *strValue = [defaults objectForKey:#"Slider"];

NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithFloat:slider.value] forKey:#"sliderValue"];
I believe you should store the value as a float (the original type for a UISlider value), it's just cleaner, and you'll probably want to reuse the float value to set back your slider when reopening the screen with something like:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[self.slider setValue:[[defaults objectForKey#"sliderValue"] floatValue]];

Related

NSUserDefaults standardUserDefaults synchronize gives wrong result objective c

I want to save high score using
synchronize
in my app,but i get gibberish number. Here what i got :
GameScene.m
if (highScore <score) {
highScore = score;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:highScore forKey:#"highScore"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
GameOver.m
NSUserDefaults *prefs =[NSUserDefaults standardUserDefaults];
NSString *rezult =[prefs stringForKey:#"highScore"];
SKLabelNode *highscorelabel = [SKLabelNode labelNodeWithFontNamed:#"Menlo-Bold"];
highscorelabel.text =[NSString stringWithFormat:#"%ld",(long)rezult];
highscorelabel.position = CGPointMake(CGRectGetMidX(self.frame), 280);
highscorelabel.fontSize = 45;
highscorelabel.zPosition = 5;
[self addChild:highscorelabel];
Why I'm getting wrong value and how to fix this problem?
You're getting a wrong value because rezult is a NSString, you should change this:
NSString *rezult =[prefs stringForKey:#"highScore"];
to this:
NSInteger rezult = [prefs integerForKey:#"highScore"];
try to don't init another nsuserdefaults every time just use
[[NSUserDefaults standardUserDefaults] setValue:surname.text forKey:#"Nume"]; to write
[[NSUserDefaults standardUserDefaults] valueForKey:#"Name"] to read
AND MAKE SURE YOU USE THE SAME OBJECT TYPE!

UILabel text not being stored in NSUserDefault

I have a UILabel called 'labelA'. In this label is the letter 'A'.
When I am using NSUserDefault and start my app, the label is empty, but I don't know why?
Here is my ViewController.m file:
- (void)viewDidLoad {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
self->labelA.text = [defaults objectForKey:#"labelAtext"]; }
- (void)defaults {
NSString *textLabelA = labelA.text;
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:textLabelA forKey:#"labelAtext"];
[defaults synchronize];
}
Here is my NSString in the ViewController.h file
NSString *textLabelA =#"A";
When i load my app without NSUserDefault the labelA.text = A.
does anybody know what's my problem. Thanks a lot :-)
Your method defaults is not being called anywhere, so the text value is never stored in NSUserDefaults.
[self defaults]; // This needs to be called somewhere before you try and access NSUserDefaults.
You should also be accessing self using dot notation, not ->
self.labelA.text = [defaults objectForKey:#"labelAText"];
-> is for dereferencing a struct in C, for more information - What is "->" in Objective C?
When you use UserDefaults. You should first test if you get a value as if the value doesnt exist you need to set it or not.
NSString * myPreference = #"labelAtext";
- (void)viewDidLoad {
NSUserDefaults* userDefault = [NSUserDefaults standardUserDefaults];
id labelValue = [userDefault objectForKey:myPreference];
if (labelValue) // if preference exist
self.labelA.text = labelValue; // set ur label
else
[self storeDefaultValue]; // you store a default value
}
- (void)storeDefaultValue
{
NSString *myDefaultValue = nil;
if ([self.labelA.text isNotEqualToString:#""]) // if ur label not empty
myDefaultValue = self.labelA.text;
else
myDefaultValue = #"Not Available"; // or whatever defaults value you want
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:myDefaultValue forKey:myPreference];
[defaults synchronize];
}
What do you really want to achieve as I don't really understand whats the point of storing a value in preference as soon as you load your app. Give more details on what is your goal.

Always getting 0 for integer retrieved from NSUserDefaults

I have an integer stored in user defaults for my game's high score. Every time I start the game I want to check if the score is bigger than the previous high score, so I do this:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:highScoreNum forKey:#"highScoreNumber"];
if (highScoreNum <= score) {
highScoreNum = score;
SKLabelNode *highLabel = (SKLabelNode *)[self childNodeWithName:#"highLabel"];
highLabel.text = [NSString stringWithFormat:#"%ld", (long)highScoreNum];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:highScoreNum forKey:#"highScoreNumber"];
}
else if (highScoreNum > score) {
SKLabelNode *highLabel = (SKLabelNode *)[self childNodeWithName:#"highLabel"];
highLabel.text = [NSString stringWithFormat:#"%ld", (long)highScoreNum];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:highScoreNum forKey:#"highScoreNumber"];
}
and at the place where I declare the variable I do this:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:highScore forKey:#"highScoreNumber"];
highLabel.text = [NSString stringWithFormat:highScore];
but I always get 0 in highScoreNum.
Every time you do this...
[defaults setInteger:highScore forKey:#"highScoreNumber"];
...you send the highScore variable to the defaults. If you declare highScore, but then don't fetch a previously saved default (or don't otherwise initialize highScore), then it makes sense why you keep seeing zero here. You probably keep sending nil to the defaults, which go in (and come back out) as an integer value of zero.
Immediately after you declare *defaults, instead of calling setInteger:forKey, you probably want to have highScore store the appropriate value from the defaults, which looks like this:
highScore = [defaults integerFromKey:#"highScoreNumber"];
Also, don't forget to [defaults synchronize] when appropriate.
setInteger:forKey::
Sets the value of the specified default key to the specified integer
value.
So your code stores a value to the NSUserDefaults, in several places. It never reads a value back.
(and you should look up -synchronize)
Try this
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:highScoreNum forKey:#"highScoreNumber"];
[defaults synchronize]; // force the upgrade NSUserDefaults
if (highScoreNum <= score) {
highScoreNum = score;
SKLabelNode *highLabel = (SKLabelNode *)[self childNodeWithName:#"highLabel"];
highLabel.text = [NSString stringWithFormat:#"%ld", (long)highScoreNum];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:highScoreNum forKey:#"highScoreNumber"];
}
else if (highScoreNum > score) {
SKLabelNode *highLabel = (SKLabelNode *)[self childNodeWithName:#"highLabel"];
highLabel.text = [NSString stringWithFormat:#"%ld", (long)highScoreNum];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:highScoreNum forKey:#"highScoreNumber"];
[defaults synchronize]; // force the upgrade NSUserDefaults
}

How to add to a saved NSUserDefault?

I am making a game in Xcode which in includes a scoring system in each level. Here I have some code that gets an NSString (passedValue1) by using a delegate.
Then i add the code to receive the value in my viewDidLoad and display my value in a UILabel
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
// Do any additional setup after loading the view, typically from a nib
NSString *key=#"labelKey";
if(passedValue1){
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSObject * object = [prefs valueForKey:key];
if(object != nil){
NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
[defults setValue:passedValue1 forKey:key];
[defults synchronize];
}
else{
NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
NSInteger readScore=[[[NSUserDefaults standardUserDefaults] valueForKey:key] integerValue];
NSInteger newValue=readScore+[passedValue1 integerValue];
[defults setValue:[NSString stringWithFormat:#"%d",newValue] forKey:key];
[defults synchronize];
}
}
label.text = [[NSUserDefaults standardUserDefaults]objectForKey:key];
}
Once I have displayed the value I then save it into a label using a NSUserDefault. However, once I have replayed my game and have another score value I would like to add the new passedValue1 value to the currently saved value...
For example:
say I play my level and I get the score value of 10. The value is then saved and I replay my level. I would then like to take the saved value and add it to the value i just scored. So thats say the second value I have scored is 20. I would then like my code to add them together and give me a value of 30.
can anyone help because the code I'm using does not correctly do the function that I want to do which is add the previous and the currently passed value.
What is wrong with my code??
Thanks in advance.
You shouldn't use valueForKey: for this (it's not what you might think it is, see Key-Value Coding). Instead, you should use objectForKey: or in your case integerForKey:.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSInteger value = [defaults integerForKey:key];
value += [passedValue1 integerValue];
[defaults setInteger:value forKey:key];
BOOL success = [defaults synchronize];
It seems that you got your if condition wrong, it should be
if (object == nil) { // <-- you have object != nil here !
// save new value
} else {
// read old value, add something, save new value
}

Set switch position to ON by first app launch using NSUserDefaults

I have switches in my app that stores position by standardUserDefaults, but when app starts for first time all my switches are in OFF position. How can I set them to ON by default?
Save
NSUserDefaults *defs1 = [NSUserDefaults standardUserDefaults];
[defs1 setBool: blackSwitch.on forKey: #"blackKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
Load
NSUserDefaults *defs1 = [NSUserDefaults standardUserDefaults];
blackSwitch.on = [defs1 boolForKey: #"blackKey"];
First of all
Do the following
NSUserDefaults *defs1 = [NSUserDefaults standardUserDefaults];
BOOL isOn = [defs1 boolForKey: #"blackKey"];
if(isOn) NSLog(#"isOn");
Second asure that blackSwitch is not nil
if(blackSwitch) NSLog(#"blackSwitch is not nil");
If blackswitch is nil, that means that you are still in early stage of view controller initialization, you should move the code that sets it on to viewDidLoad or viewWillAppear
For setting default values please use this code
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSNumber *testValue = [NSNumber numberWithBool:YES];
NSDictionary *appDefaults = [[NSDictionary alloc] initWithObjectsAndKeys:
testValue, #"blackKey", nil];
[defaults registerDefaults:appDefaults];
[appDefaults release];
Use the NSUserDefaults method registerDefaults to pass a default value of 'true' along with your #"blackKey" key. This will ensure that #"blackKey" is set to ON until the user makes a change to it themselves.

Resources