我试图保存高分,我有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];
}
答案 0 :(得分:0)
问题是hs.text
被调用时,viewDidLoad
将不会被设置(或者如果你在xib / Storyboard中那样设置为0)。因此,它始终进入第一个if语句,并且您不使用NSUserDefaults
中存储的值。
我建议按如下方式设置文本字段:
- (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 (highscore)
hs.text = [NSString stringWithFormat:@"%.4f", highscore];
if (highscore1)
hs1.text = [NSString stringWithFormat:@"%.4f", highscore1];
if (highscore2)
hs2.text = [NSString stringWithFormat:@"%.4f", highscore2];
if (highscore3)
hs3.text = [NSString stringWithFormat:@"%.4f", highscore3];
if (highscore4)
hs4.text = [NSString stringWithFormat:@"%.4f", highscore4];
...
此外,我认为saveScore
方法无法正常工作,因为它将所有高分设置为finTime
。您可能希望将它们设置为每个hs
文本字段的浮点值。