私はハイスコアを保存しようとしています.5つのハイスコア用に5つのテキストボックスがあり、彼らが負けたときにそれらを交換したいのは明らかです. しかし、私にとっては常にifステートメントにif ([hs.text floatValue] == 0)
入ります。何度実行しても、最初のハイスコアテキストボックスを置き換えるだけです。私は何を間違っていますか?
- (void)viewDidLoad
{
[super viewDidLoad];
highscore = [[NSUserDefaults standardUserDefaults] floatForKey: @"highScore"];
highscore1 = [[NSUserDefaults standardUserDefaults] floatForKey: @"highScore1"];
highscore2 = [[NSUserDefaults standardUserDefaults] floatForKey: @"highScore2"];
highscore3 = [[NSUserDefaults standardUserDefaults] floatForKey: @"highScore3"];
highscore4 = [[NSUserDefaults standardUserDefaults] floatForKey: @"highScore4"];
if ([hs.text floatValue] == 0) {
hs.text = [NSString stringWithFormat:@"%.4f",finTime];
}
else if ([hs1.text floatValue] == 0) {
hs1.text = [NSString stringWithFormat:@"%.4f",finTime];
}
else if ([hs2.text floatValue] == 0) {
hs2.text = [NSString stringWithFormat:@"%.4f",finTime];
}
else if ([hs3.text floatValue] == 0) {
hs3.text = [NSString stringWithFormat:@"%.4f",finTime];
}
else if ([hs4.text floatValue] == 0) {
hs4.text = [NSString stringWithFormat:@"%.4f",finTime];
}
else if (finTime < highscore) {
hs4.text = hs3.text;
hs3.text = hs2.text;
hs2.text = hs1.text;
hs1.text = hs.text;
hs.text = [NSString stringWithFormat:@"%.4f",finTime];
}
else if (finTime < highscore1) {
hs4.text = hs3.text;
hs3.text = hs2.text;
hs2.text = hs1.text;
}
else if (finTime < highscore2) {
hs4.text = hs3.text;
hs3.text = hs2.text;
hs2.text = [NSString stringWithFormat:@"%.4f",finTime];
}
else if (finTime < highscore3) {
hs4.text = hs3.text;
hs3.text = [NSString stringWithFormat:@"%.4f",finTime];
}
else if (finTime < highscore4) {
hs4.text = [NSString stringWithFormat:@"%.4f",finTime];
}
[self saveScore];
}
-(void) saveScore
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setFloat:finTime forKey:@"highScore"];
[defaults setFloat:finTime forKey:@"highScore1"];
[defaults setFloat:finTime forKey:@"highScore2"];
[defaults setFloat:finTime forKey:@"highScore3"];
[defaults setFloat:finTime forKey:@"highScore4"];
[[NSUserDefaults standardUserDefaults] synchronize];
}