我創建了一個問答遊戲。這裏是我的代碼的一部分:用NSUserDefaults保存數據
- (void) viewDidLoad
{
logos = [[NSMutableArray alloc] init];
[logos addObject:@"adidas"];
[logos addObject:@"nike"];
[logos addObject:@"puma"];
[self showLogo];
}
int currentQuestionIndex;
- (void) showLogo
{
currentQuestionIndex++;
NSString *question = [logos objectAtIndex:currentQuestionIndex];
[_questionImage setImage:[UIImage imageNamed:question]];
...
}
如果用戶選擇正確的答案,將再次調用「showLogo」去下一個問題。所以,等級完成。
一切都很好。
請幫我保存信息/級別。中頻水平完成後,當用戶啓動遊戲時,他必須從保存的水平開始。
這是我已經試過:
- (void) checkAnswer
{
///... if user answered right
[[NSUserDefaults standardUserDefaults] setInteger:currentQuestionIndex forKey:@"currentLevel"];
[self showLogo];
/// so, I tried to save "currentQuestionIndex" forKey currentLevel, and call "showLogo"
}
,這裏是修改「showLogo」
- (void) showLogo
{
int savedLevel = [[NSUserDefaults standardUserDefaults] integerForKey:@"currentLevel"];
currentQuestionIndex = savedLevel+1 ;
NSString *question = [logos objectAtIndex:currentQuestionIndex];
[_questionImage setImage:[UIImage imageNamed:question]];
...
}
,但使用這種方法是行不通的..,我想救得分,和它的作品也..
請幫助。由於
我在我的答案中嘗試了代碼,它的工作原理。 – tolgamorf 2013-03-05 03:08:01
我認爲問題出在我的觀點上。它可以工作,但是當我重新啓動應用程序時,每當我的等級提高時都不回答問題。 :)所以,我的NSUserDefaults保存當前的水平。當我重新啓動應用程序時,我的代碼將讀取NSUserDefaults ++,並自動進入下一級,同時保存當前級別並再次... – 2013-03-05 03:18:40