我遇到NSUserdefaults問題。當我在文本框中輸入名稱並單擊高分時,應用程序將不會響應任何內容,並且鍵盤仍將保留在屏幕上。當我試圖加載數據時,它說名稱的數據是零。我的得分是90.有人可以告訴我我的編碼有什麼問題。數據不保存在NSUserDefaults中
非常感謝。
-(IBAction)savehighscore_button {
int i, ii = -1;
struct high_score {
NSString *name;
int highScore;
};
struct high_score structArray[10];
NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
for (i=0; i<10; i++) {
if ([userPreferences stringForKey :[NSString stringWithFormat:@"highScoreNameEntry%d",i]]!=nil && [userPreferences stringForKey :[NSString stringWithFormat:@"highScoreEntry%d"]]!=nil) {
structArray[i].name= [userPreferences stringForKey:[NSString stringWithFormat:@"highScoreNameEntry%d",i]];
structArray[i].highScore = [userPreferences integerForKey:[NSString stringWithFormat:@"highScoreEntry%d",i]];
ii = i;
}
}
if (myScore >0) {
for (i==ii; i>=0; i--) {
if (myScore > structArray[i].highScore) {
if (i<9) {
structArray[i+1] = structArray[i];
structArray[i].name = nametextbox.text;
structArray[i].highScore = myScore;
if (i==ii && i<9) {
structArray[i+1].name = nametextbox.text;
structArray[i+1].highScore = myScore;
ii=i+i;
}
else if(i==ii && i<9) {
structArray[i+1].name = nametextbox.text;
structArray[i+1].highScore = myScore;
ii=i+1;
}
}
}
if (ii==-1 && myScore >0) {
structArray[0].name = nametextbox.text;
structArray[0].highScore = myScore;
ii=0;
}
for (i=0; i<=ii; i++) {
[userPreferences setObject:structArray[i].name forKey:[NSString stringWithFormat:@"highScoreNameEntry%d",i]];
[userPreferences setInteger:structArray[i].highScore forKey:[NSString stringWithFormat:@"highScoreEntry%d",i]];
}
}
}
}
喜卡爾,可我知道ü編輯哪個部分。 – lol 2010-12-01 18:25:28
他修復了你破碎的代碼格式 – vikingosegundo 2010-12-01 18:28:08