0

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

}
4

1 回答 1

0

问题是在被调用hs.text时不会被设置(或者如果你在 xib/Storyboard 中这样做,将被设置为 0)viewDidLoad。因此,它总是进入第一个 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文本字段中的浮点值。

于 2013-07-18T15:38:09.153 回答