[英]IOS/Objective-C: Can't match users input to Array(index)
我正在學習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”方法進行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.