我试图保存高分,我有 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];
}