-1

我有一个NSMutableArray包含问题的答案。答案存储在NSString. 当用户单击键盘上的完成按钮时,我正在尝试检查答案是否正确。

每次我点击完成时,它总是显示不正确,而实际上答案是正确的。

我注意到我做错了什么。

我什至做了一个NSLog并且NSLog返回了有效的答案。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 回答