0

我有一个基于选项卡的应用程序,并且每次在选项卡之间切换时都想重新加载数据。所以我使用viewWillAppear:了 which 被调用但数据不会重新加载。这可能是使用大量[NSUserDefault]'s 的结果吗?这是我的代码:

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self reloadData];
}

-(void)reloadData {

    //Loading all the data
    totalApples = [[NSUserDefaults standardUserDefaults]integerForKey:@"totalApples"];
    [self setText:[NSString stringWithFormat:@"%.01f", totalApples] withExistingAttributesInLabel:self.applesLabel];

    applesPerSecond = [[NSUserDefaults standardUserDefaults]integerForKey:@"applesPerSecond"];
    [self setText:[NSString stringWithFormat:@"%.01f", applesPerSecond] withExistingAttributesInLabel:self.applesPerSecondLabel];

    applesPerClick = [[NSUserDefaults standardUserDefaults]integerForKey:@"applesPerClick"];
}
4

2 回答 2

2

使用时最好在写入数据后NSUserDefaults调用。-synchronize

举个例子:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

[defaults setObject:@100 forKey:@"SomeKey"];

[defaults synchronize]; //This will ensure data is written.

如果您不调用此方法,系统会定期调用。但是,它可能会导致您可能遇到的时间问题。

在此处阅读有关NSUserDefaults此行为的更多信息。

更新

我认为这个问题现在正在失去它的背景。这是您在下面的评论中发布的更新方法。可能值得发布一个后续问题,因为这可能不是NSUserDefaults.

这里至少对于后代来说是一个使用NSUserDefaults写出数据的例子。由于正在执行乘法,我还更新了您的方法以将priceFarmHands保存为浮点数。(它之前被写为整数,可能被截断)。

- (void)buyFarmhand {
    farmHands++;
    totalApples -= priceFarmHands;

    if(priceFarmHands < 1000) {
        priceFarmHands = priceFarmHands * 1.35;
    } else if(priceFarmHands < 10000) {
        priceFarmHands = priceFarmHands * 1.25;
    } else {
        priceFarmHands = priceFarmHands * 1.15;
    }

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setFloat:priceFarmHands forKey:@"priceFarmHands"];
    [defaults setInteger:farmHands forKey:@"farmHands"];
    [defaults setDouble:totalApples forKey:@"totalApples"];

    [defaults synchronize];

    [self setText:[NSString stringWithFormat:@"%.f", priceFarmHands] withExistingAttributesInLabel:self priceFarmHandsLabel];
    [self setText:[NSString stringWithFormat:@"%d", farmHands] withExistingAttributesInLabel:self.numberFarmhands];
}
于 2013-09-08T17:02:52.323 回答
0

我看不到您在哪里创建 NSUserdefault 对象,它应该在 reloadData 方法中创建,这应该可以工作:

-(void)reloadData {
    //Loading all the data
    NSUserDefaults *appleData = [NSUserDefaults standardUserDefaults];
    totalApples = [appleData integerForKey:@"totalApples"];
    applesPerSecond = [appleData integerForKey:@"applesPerSecond"];
    applesPerClick = [integerForKey:@"applesPerClick"];

    //Set the labels text
    [self setText:[NSString stringWithFormat:@"%.01f", totalApples] 
                    withExistingAttributesInLabel:self.applesLabel];
    [self setText:[NSString stringWithFormat:@"%.01f", applesPerSecond] 
               withExistingAttributesInLabel:self.applesPerSecondLabel];
}

另外为了帮助您解决此问题,请添加一些日志viewWillAppear

-(void)viewWillAppear:(BOOL)animated{
    NSLog(@"viewWillAppear called, reloadind data...");
    [super viewWillAppear:animated];
    [self reloadData];
}
于 2013-09-08T23:13:49.713 回答