2012-12-28 123 views
-1

我有一個NSMutableArray,它包含一個問題的答案。答案存儲在NSString中。當用戶點擊鍵盤上的完成按鈕時,我試圖檢查答案是否正確。檢查NSMutableArray中NSString的正確答案

每當我點擊完成,它總是顯示不正確的時候,實際上,答案是正確的。

我記下我做錯了什麼。

我什至做了NSLogNSLog返回有效的答案。 if語句有錯嗎?

代碼:

- (IBAction)checkAnswer:(UITextField *)sender 
{ 
    NSLog(@"The question's answer for the question you selected is %@", [selectedQuestion questionAnswer]); 

     if (answerField.text == [selectedQuestion questionAnswer]) 
     { 
      // Show the correct label 
      [correctLabel setHidden:NO]; 
      correctLabel.text = @"Correct!"; 
      correctLabel.textColor = [UIColor greenColor]; 

     } 
     if (answerField.text != [selectedQuestion questionAnswer]) 
     { 
      [correctLabel setHidden:NO]; 
      correctLabel.text = @"Incorrect"; 
      correctLabel.textColor = [UIColor redColor]; 
     } 

     // answerField.text = @""; 
} 

回答

0

必須使用NSString類的梅索德isEqualToString

- (IBAction)checkAnswer:(UITextField *)sender 
{ 


NSLog(@"The question's answer for the question you selected is %@", [selectedQuestion questionAnswer]); 

    if ([answerField.text isEqualToString:[selectedQuestion questionAnswer]]) 
    { 
     // Show the correct label 
     [correctLabel setHidden:NO]; 
     correctLabel.text = @"Correct!"; 
     correctLabel.textColor = [UIColor greenColor]; 

    } 
    if (answerField.text != [selectedQuestion questionAnswer]) 
    { 
     [correctLabel setHidden:NO]; 
     correctLabel.text = @"Incorrect"; 
     correctLabel.textColor = [UIColor redColor]; 
    } 

    // answerField.text = @""; 

}

+0

謝謝你,哇,我真是愚蠢。我忘記了那個聲明。我會在8分鐘內接受你的回答,它不會讓我接受我的答案。 –

+0

不客氣 – iArezki