0

在我的测验中,我有几个答案。正确答案在 plist 中。这里是

<array>
<dict>
    <key>QuestionTitle</key>
    <string>Веки являются</string>
    <key>Answers</key>
    <array>
        <string>частью глазного яблока</string>
        <string>защитным аппаратом органа  зрения</string>
        <string>и тем, и другим</string>
        <string>ни тем, ни другим</string>
    </array>
    <key>CorrectAnswer</key>
    <array>
        <integer>0</integer>
        <integer>1</integer>
    </array>
</dict>

现在我无法猜测如何检查用户回答的答案我可以为 A、B、C、D 答案创建变量 0、1、2、3,但我应该如何将它与 CorrectAnswer Array 进行比较有我的 m。文件代码

NSString* path = [[NSBundle mainBundle] pathForResource:@"Questions2" ofType:@"plist"];
NSMutableArray *questionsDict = [[NSMutableArray alloc] initWithContentsOfFile:path];
 NSUInteger count = [questionsDict count];
for (NSUInteger i = 0; i < count; ++i)
{
    int nElements = count - i;
    int n = (arc4random()% nElements) + i;
    [questionsDict exchangeObjectAtIndex:i withObjectAtIndex:n];
}
self.questions = [questionsDict copy];
currentQuestion = 0;
NSDictionary* nextQuestion = 
[self.questions objectAtIndex:   currentQuestion];//[self.questions objectForKey:
[NSString    stringWithFormat:@"%d", currentQuestion]];
NSMutableArray *array = [nextQuestion[@"Answers"] mutableCopy];
self.labelA.text = [array objectAtIndex:0];
self.labelB.text = [array objectAtIndex:1];
self.labelC.text = [array objectAtIndex:2];
self.labelD.text = [array objectAtIndex:3];

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

    //Button D (A,B,C the same)
      - (IBAction)fourButton:(id)sender
    {
    if (chekColorD == 0)
    {
    chekColorD++;
    [buttonD setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    //self.stringD = @"D";
    }
    else
    {
    chekColorD = 0;
    [buttonD setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
   // self.stringD = nil;     
   }
   }

    //NExt question button
   - (IBAction)nextQuestion:(id)sender
      {
     // Тут делаешь сравнение правильных ответов    
     chekColorA = 0; chekColorB = 0; chekColorC = 0; chekColorD = 0;
    [buttonA setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [buttonB setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [buttonC setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [buttonD setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    self.labelScore.text = [NSString stringWithFormat:@"%d", numCorrect];

    if ([self.highScore integerForKey:@"HighScore"]<numCorrect){
    [self.highScore setInteger:numCorrect forKey:@"HighScore"];
    [self.highScore synchronize];
} 
currentQuestion++; 
if (currentQuestion <= [self.questions count]){

    self.labelScore.text = [NSString stringWithFormat:@"%d", numCorrect];
        self.labelHighestScore.text = [NSString stringWithFormat:@"%d", 
   [self.highScore   integerForKey:@"HighScore"]];

    NSDictionary* nextQuestion = [self.questions objectAtIndex: currentQuestion];
    NSMutableArray *array = [nextQuestion[@"Answers"] mutableCopy]; 
    self.labelA.text = [array objectAtIndex:0];
    self.labelB.text = [array objectAtIndex:1];
    self.labelC.text = [array objectAtIndex:2];
    self.labelD.text = [array objectAtIndex:3];

    self.labelQuestion.text = [nextQuestion objectForKey:@"QuestionTitle"];
    //NSLog(@"%d количество вопросов", countQuestion);
}
else{

    currentQuestion --;
}
}
4

1 回答 1

0

您可以像这样比较两个字符串:

if ([@"string1" isEqualToString:@"string2"]) {
    // YES it s the same
}
else{
    // no it s not the same

}

例如 [labelA.text isEqualToString:correctAnswerString]。我建议您制作答案按钮:button1:firstAnswer,button2:secondAnswer,依此类推。当用户点击它时,您可以捕捉到发件人,这将是 a UIButton,如果您为答案设置按钮的标题,您可以比较(UIButton*)sender.titleLabel.text isEqualToString:correctAnswer

    // the question is: Lion is a...

    UIButton* button1 = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
    UIButton* button2 = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
    UIButton* button3 = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
    UIButton* button4 = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];

    [button1 setTitle:@"animal" forState:UIControlStateNormal];
    [button2 setTitle:@"car" forState:UIControlStateNormal];
    [button3 setTitle:@"tv" forState:UIControlStateNormal];
    [button4 setTitle:@"radio station" forState:UIControlStateNormal];

    [button1 addTarget:self action:@selector(checkAnswer:) forControlEvents:UIControlEventTouchUpInside];
    [button2 addTarget:self action:@selector(checkAnswer:) forControlEvents:UIControlEventTouchUpInside];
    [button3 addTarget:self action:@selector(checkAnswer:) forControlEvents:UIControlEventTouchUpInside];
    [button4 addTarget:self action:@selector(checkAnswer:) forControlEvents:UIControlEventTouchUpInside];


-(bool) checkAnswer:(id)sender{
    NSString* correctString = @"animal";
    UIButton* tappedButton = (UIButton*)sender;
    if ([tappedButton.titleLabel.text isEqualToString:correctString]) {
        return YES;
    }
    return NO;
}
于 2013-09-16T11:58:23.763 回答