tags:

views:

432

answers:

6

i have a integer called HighScore which is connected to highscorelabel . i have made it so when the user gets a high score it puts the score they got onto the label but i would now like to know how i can save it so that when the app is opened again it will still have the high score : this is my code for detecting when a high score is made

  • (void) submitScore { if (lives > HighScore){ HighScore = lives; } highscorelabel.text = [NSString stringWithFormat:@"%i" , HighScore]; }

can anyone help me please thankyou dave

A: 

Use NSUserDefaults for saving:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

[prefs setInteger:HighScore  forKey:@"HighScore "];

[prefs synchronize];

and retrieving:

HighScore  = [prefs integerForKey:@"HighScore"];
luvieere
A: 

hi thanks for your quick responce , it all succeeds with no errors but terminates when the method is called :/ heres what i have done . am i doing it wrong ?

 - (void) submitScore {
if (lives > HighScore){
 HighScore = lives;
}
{
 NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
 [prefs setInteger:HighScore  forKey:@"HighScore "];
 [prefs synchronize];
}
{
 NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
 HighScore  = [prefs integerForKey:@"HighScore"];
}
highscorelabel.text = [NSString stringWithFormat:@"%i" , HighScore];

}

do i need to @synthesize any of my labels or anything ? thankyou.

What do you mean by "terminates"? In console, do you see any error message? If you try to set the label's text to anything else, does it still crash? If by any chance HighScore declared as double or anything except integer?
luvieere
A: 

the aplication freezes and in xcode it says somehting about THE_APPLICATION_HAD_TO_TERMNATE or somehting llike that ... ummm well the label responds to when i have my original code:

- (void) submitScore {
if (lives > HighScore){
 HighScore = lives;
}
highscorelabel.text = [NSString stringWithFormat:@"%i" , HighScore];

}

but i dont no really... by the way i presumed that a int is a integer because that is what my HighScore is ... in my .h i have declared it as:

int HighScore;

is this correct.

thanks again

Make sure the keys for setInteger and integerForKey match... in my initial post I've accidentally left an extra white space: @"HighScore " instead of :@"HighScore". If everything else fails, please add a copy af the full crash text in xcode - THE_APPLICATION_HAD_TO_TERMINATE ... I'm interested in the reason it specifies
luvieere
A: 

i think that the space was it .

it dose not crash anymore but instead dosnt save the score for when i open up the app again or just simply change the text to the high score

i was wondering if it has anything to do with the %i part of this

highscorelabel.text = [NSString stringWithFormat:@"%i" , HighScore];

??? i have seen many different ways of which people do this but i dont know what would be right for me.

Make sure that you retrieve the saved score and update the text on the label BEFORE you save the new score. Just as you open the app, retrieve the former score and update the label, and then, on submitScore, save the new one.
luvieere
A: 

how do i update my label?

A: 

:):):) i have just done what you said by putting the load method first and it works!!!!!!!! finally my app is complete :):) i only need to restyle it now ... i cannot thank you enough i have been trying to get this part of my app working for nearly a week now and no one could explain it to me as well as you did .

thankyou again, Dave