I have two storyboards in my game: the home page and the game itself. When a user gets a high score in the game i save it as their high score. How can i then take that high score to display it on the homepage without linking the view controllers to the same code.
Save highscore anywhere in your project
[[NSUserDefaults standardUserDefaults] setInteger:score forKey:#"kHighscoreKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
Retrieve highscore anywhere else in your project
NSUInteger highScore = [[NSUserDefaults standardUserDefaults] integerForKey:#"kHighscoreKey"];
If your do not find your score data important or sensitive use NSUserDefaults
First scene:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:yourHighScore forKey:#"HighScore"];
Second scene:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
yourHighScore = [defaults integerForKey:#"HighScore"]
Related
For Saving the data
NSString *balSnapTokenFromResponce;
balSnapTokenFromResponce = #"456294797493749873";
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:balSnapTokenFromResponce forKey:#"Token"];
[defaults synchronize];
For Retreiving the Data
[[NSUserDefaults standardUserDefaults] stringForKey:#"Token"]
This, as shown, makes no sense:
[[NSUserDefaults standardUserDefaults] stringForKey:#"Token"]
You must do something with the return value:
NSString *balSnapTokenFromResponce = [[NSUserDefaults standardUserDefaults] stringForKey:#"Token"];
Perhaps you are, but that's not clear from your question. Because otherwise the code you've posted should work, assuming it's getting called.
I am making a small game for iOS in Xcode. In my game, tapping on a dot will give you one point. I am trying to make it so that if your score is higher than the first high score(0), it should save that and display the new high score. And the next score can beat that, and so on. Please give a thorough explanation because I am new to xCode and I am a 12 year old app developer.
If your do not find your score data important or sensitive use NSUserDefaults
First scene: (Setting the integer)
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:yourHighScore forKey:#"HighScore"];
Other scenes: (Getting the integer)
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
yourHighScore = [defaults integerForKey:#"HighScore"]
Or same scene setting the yourHighScore value: (Getting the integer)
-(void)viewDidLoad {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
yourHighScore = [defaults integerForKey:#"HighScore"]
}
NSInteger HighScore;
int Score;
IBOutlet UILabel *Show;
-(void)ViewDidLoad
{
[super viewDidLoad];
HighScoreNumber = [[NSUserDefaults standardUserDefaults] integerForKey:#"HighScoreSaved"];
}
-(void)ShowHighScore
{
if (Score > HighScore) {
HighScore = Score;
[[NSUserDefaults standardUserDefaults] setInteger:HighScore forKey:#"HighScoreSaved"];
}
Show.text = [NSString stringWithFormat:#"%li", HighScore];
}
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
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can I save and load the alpha values of a UIButton in an app?
I would like to save the state of the UIButton (e.g. its alpha value and whether it is hidden or not) and this would then load up when the user quits and reloads the app.
I've tried some bits of code with NSUserDefaults but with no luck.
Could somebody help with some sample code so that I can save and load the button's state?
Thanks,
James
Related to Shaharyar's answer (i don't know how to comment):
in this case you need to use NSNumber.
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithFloat:SOME_FLOAT] forKey:KEY];
because float is not an object, but NSNumber is one.
EDITED:
1) To make sure your defaults are created after the application runs at first time:
in your AppDelegate's initialize-method:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:SOME_FLOAT], #"YOUR_KEY",
nil];
[defaults registerDefaults:appDefaults];
2) Updating defaults after:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setFloat:FLOAT_VALUE forKey:#"YOUR_KEY"];
[prefs synchronize];
3) Read defaults:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
float FLOAT_VALUE = [prefs floatForKey:#"YOUR_KEY"];
Can you post some of the code?
NSUserDefaults is the place to store such information..
Assumption:
Did you make a call to [NSUserDefaults synchronize] after setting the values?
Code:
// Setting a value
[[NSUserDefaults standardUserDefaults] setValue:VALUE forKey:KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
// Getting a value
NSString *var1 = [[NSUserDefaults standardUserDefaults] valueForKey:KEY];
In your case it would be:
// Setting a value
[[NSUserDefaults standardUserDefaults] setFloat:VALUE forKey:KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
I have the following two procedures defined in my AppDelegate. saveSettings and loadSettings. I am calling my loadSettings procedure in the AppDidFinishLaunching method, and I am calling the saveSettings procedure in my settings view, once the save button is clicked.
Both methods seem to be called at the right time, the right number of times (once), and using the correct data. my settings object gets the right data, but the data does not seem to be actually saving. When I run the load code, my resulting variables are coming back empty (not nil).
I tried putting the same loading code in a different view and it works fine, but for some reason, I am not getting results in my appDelegate.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
[self loadSettings];
[self setDefaults];
}
-(void)loadSettings{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
settings.masterLocation = [prefs objectForKey:#"masterLocation"];
settings.masterPort = [prefs objectForKey:#"masterPort"];
settings.userName = [prefs objectForKey:#"userName"];
settings.passWord = [prefs objectForKey:#"passWord"];
settings.autoLogin=[prefs objectForKey:#"autoLogin"];
if (settings.autoLogin == nil)
settings.autoLogin=#"N";
}
-(void)saveSettings:(SharedData *)d{
settings=d;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:settings.masterLocation forKey:#"masterLocation"];
[prefs setObject:settings.masterPort forKey:#"masterPort"];
[prefs setObject:settings.userName forKey:#"userName"];
[prefs setObject:settings.passWord forKey:#"passWord"];
[prefs setObject:settings.autoLogin forKey:#"autoLogin"];
}
Doh.
In saveSettings, I was missing my [prefs synchronize];
to make it sample:
//Writing
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:#"anInt"];
[[NSUserDefaults standardUserDefaults] setDouble:1.2 forKey:#"aDouble"];
[[NSUserDefaults standardUserDefaults] setString:#"aString" forKey:#"aString"];
[[NSUserDefaults standardUserDefaults] synchronize];
//Reading:
int i = [[NSUserDefaults standardUserDefaults] integerForKey:#"anInt"]