IOS plist简单测验游戏

时间:2013-02-24 13:33:20

标签: objective-c plist

嗨,我一直在尝试创建一个问答游戏,我一直在寻找答案,但没有运气。我正在创建一个测验,其中读取带有许多问题和答案的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 作为键,但我似乎无法更改根名称如果有人知道如何改变这个问题,我相信这可能会解决我的问题?我编辑了代码以解决这个问题,这是我的 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
    }
}

非常感谢您提供的任何帮助

1 个答案:

答案 0 :(得分:-1)

self.lblQuestionTitle.text = [nextQuestion objectForKey:@"QuestionTitle"];