嗨,我一直在尝试创建一个问答游戏,并且到处寻找答案,但没有运气。我正在创建一个测验,它读入一个包含许多问题和答案的 plist 文件,一切正常,除了当我选择答案时,下一个问题不会出现。通过使用 NSLogs,我可以看到 currentQuestion 值的增量有效,实际上似乎正在丢失 plist 文件的 self.question 是我的 plist 代码,因为我还无法发布图像。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Questions</key>
<array>
<dict>
<key>QuestionTitle</key>
<string>What is 1 + 1?</string>
<key>A</key>
<string>4</string>
<key>B</key>
<string>3</string>
<key>C</key>
<string>7</string>
<key>D</key>
<string>2</string>
<key>QuestionAnswer</key>
<string>D</string>
</dict>
<dict>
<key>QuestionTitle</key>
<string>What is 2 + 2?</string>
<key>A</key>
<string>4</string>
<key>B</key>
<string>3</string>
<key>C</key>
<string>7</string>
<key>D</key>
<string>2</string>
<key>QuestionAnswer</key>
<string>A</string>
</dict>
<dict>
<key>QuestionTitle</key>
<string>What is 5 + 2?</string>
<key>A</key>
<string>4</string>
<key>B</key>
<string>3</string>
<key>C</key>
<string>7</string>
<key>D</key>
<string>2</string>
<key>QuestionAnswer</key>
<string>C</string>
</dict>
<dict>
<key>QuestionTitle</key>
<string>What is 9 - 2?</string>
<key>A</key>
<string>4</string>
<key>B</key>
<string>3</string>
<key>C</key>
<string>7</string>
<key>D</key>
<string>2</string>
<key>QuestionAnswer</key>
<string>C</string>
</dict>
<dict>
<key>QuestionTitle</key>
<string>What is 10 - 6?</string>
<key>A</key>
<string>4</string>
<key>B</key>
<string>3</string>
<key>C</key>
<string>7</string>
<key>D</key>
<string>2</string>
<key>QuestionAnswer</key>
<string>A</string>
</dict>
<dict>
<key>QuestionTitle</key>
<string>What is 1 + 2?</string>
<key>A</key>
<string>4</string>
<key>B</key>
<string>3</string>
<key>C</key>
<string>7</string>
<key>D</key>
<string>2</string>
<key>QuestionAnswer</key>
<string>B</string>
</dict>
</array>
我创建此测验所遵循的教程将问题作为主键,其中我将Root作为键,但我似乎无法更改 Root 名称我相信如果有人知道如何更改它可能会解决我的问题?我编辑了代码来解决这个问题,这是我的viewDidLoad和showNextQuestion注意:当按下任何按钮 A、B、C 或 D 时,会调用 showNextQuestion。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *path = [[NSBundle mainBundle] pathForResource:@"Questions1" ofType:@"plist"];
NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.questions = [tempDict objectForKey:@"Questions"];
currentQuestion = -1;
NSLog(@"%d",currentQuestion);
[self showNextQuestion];
}
-(void)showNextQuestion
{
currentQuestion++;
NSLog(@"%d",currentQuestion);
NSLog(@"%d",[self.questions count]);
if(currentQuestion < [self.questions count])
{
NSLog(@"Inside the loop");
NSDictionary *nextQuestion = [self.questions objectAtIndex:currentQuestion];
self.answer = [nextQuestion objectForKey:@"QuestionAnswer"];
self.lblQuestion.text = [nextQuestion objectForKey:@"QuestionTitle"];
self.lblQuestionA.text = [nextQuestion objectForKey:@"A"];
self.lblQuestionB.text = [nextQuestion objectForKey:@"B"];
if([nextQuestion objectForKey:@"C"]){
self.lblQuestionC.hidden = NO;
self.btnC.hidden = NO;
self.lblQuestionC.text = [nextQuestion objectForKey:@"C"];
}
else{
self.lblQuestionC.hidden =YES;
self.btnC.hidden = YES;
}
if([nextQuestion objectForKey:@"D"]){
self.lblQuestionD.hidden = NO;
self.btnD.hidden = NO;
self.lblQuestionD.text = [nextQuestion objectForKey:@"D"];
}
else{
self.lblQuestionD.hidden =YES;
self.btnD.hidden = YES;
}
}
else{
//Game Over
}
}
对此的任何帮助将不胜感激提前谢谢您