2013-09-16 59 views
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陣列 有我的米。文件代碼

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 --; 
} 
} 

回答

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等。當它的用戶水龍頭,你可以趕上發件人,這將是一個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; 
} 
+0

OK,但如果用戶接聽第一個「B」(1 )然後「A」(0)字符串將是1,0但CorrectAnswer數組有整數0,1 –

+0

比較例如[labelA.text isEqualToString:correctAnswerString] – incmiko

+0

我編輯我的答案使其可以理解 – incmiko