-1

質問へ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 = @"";
}
4

1 に答える 1

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 = @"";

}

于 2012-12-28T20:53:05.517 に答える