我正在学习IOS/Objective-C,我让这个用户输入Quizz项目;目前我可以显示所有问题和答案(带有按钮和标签)。但至于下一步 - 用户输入答案并获得正确或错误 - 缺少一些东西;我似乎无法将用户的输入与索引相匹配。如果我写东西,输出总是"错误"。代码:
#import "QuizzViewController.h"
#import "Question.h"
@interface QuizzViewController ()
@property (weak, nonatomic) IBOutlet UILabel *questionLabel;
@property (weak, nonatomic) IBOutlet UILabel *answerLabel;
@property NSArray *questions;
@property NSUInteger index;
@property (weak, nonatomic) IBOutlet UILabel *answerValidation;
@property (weak, nonatomic) IBOutlet UITextField *inputAction;
@property (weak, nonatomic) IBOutlet UIButton *answerButton;
@end
@implementation QuizzViewController
- (void)viewDidLoad {
[super viewDidLoad];
_questions = @[
[Question question:@"Best surfer in the World?" answer:@"Kelly Slater"],
[Question question:@"Capital of England?" answer:@"London"],
[Question question:@"Capital of Argentina?" answer:@"Buenos Aires"]
];
_index = -1;
[self nextQuestionAction];
}
- (IBAction)answerAction:(UIButton *)sender {
Question *question = [_questions objectAtIndex:_index];
NSString *correct=@"Correct";
NSString *wrong=@"Wrong";
_inputAction.text=question.answer;
if (question.answer==_inputAction.text){
_answerValidation.text=correct;
}else{
_answerValidation.text=wrong;
}
}
/*- (IBAction)inputAction:(UITextField *)sender {
Question *question = [_questions objectAtIndex:_index];
NSString *correct=@"Correct";
NSString *wrong=@"Wrong";
if (question.answer==_inputAction.text){
_answerValidation.text=correct;
}else{
_answerValidation.text=wrong;
}
}*/
- (IBAction)nextQuestionAction {
_index = (_index + 1) % _questions.count;
Question *question = [_questions objectAtIndex:_index];
_questionLabel.text = question.question;
_answerLabel.text = @"...";
}
- (IBAction)showAnswerAction {
Question *question = [_questions objectAtIndex:_index];
_answerLabel.text = question.answer;
}
@end
我想我需要指向索引,但我尝试了不同的方式,但它不起作用......感谢您的帮助
答案,在Larme的帮助下:
- (IBAction)answerAction:(UIButton *)sender {
Question *question = [_questions objectAtIndex:_index];
NSString *correct=@"Correct";
NSString *wrong=@"Wrong";
_inputAction.text=question.answer;
if ([question.answer isEqualToString:_inputAction.text]){
_answerValidation.text=correct;
}else{
_answerValidation.text=wrong;
}
}
必须使用"isEqualToString"方法进行比较