这个有一些困难。我正在从 plist 加载问题、答案和正确答案。所有数据都被很好地读取,如下所示
- (void)viewDidLoad
{
[super viewDidLoad];
rootArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"question" ofType:@"plist"]];
currentQuestion = -1;
[self showNextQuestion];
}
-(void) showNextQuestion{
currentQuestion++;
int numItems = [rootArray count];
NSMutableArray *question = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *A = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *B = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *C = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *addimage = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *Answer = [NSMutableArray arrayWithCapacity:numItems];
for (NSDictionary *itemData in rootArray) {
[question addObject:[itemData objectForKey:@"question"]];
[A addObject:[itemData objectForKey:@"A"]];
[B addObject:[itemData objectForKey:@"B"]];
[C addObject:[itemData objectForKey:@"C"]];
[addimage addObject:[itemData objectForKey:@"ImageUse"]];
[Answer addObject:[itemData objectForKey:@"ANS"]];
}
self.questionasked.text = question[currentQuestion];
self.answer1.text = A[currentQuestion];
self.answer2.text = B[currentQuestion];
self.answer3.text = C[currentQuestion];
additionImage.image = [UIImage imageNamed:addimage[currentQuestion]];
self.correctAns = Answer[currentQuestion];
if(currentQuestion == 4){
[ self performSegueWithIdentifier:@"levelclear" sender:nil];// here I am performing a segue to indicate that when 4 questions are displayed it will go to this view controller
}
}
我的 VC 中有 3 个按钮,每个选项 A、B、C 一个,它们都有一个功能
- (IBAction)SelectA:(id)sender {
if ([self.correctAns isEqualToString:@"A"]) {
currentQuestion ++;
[self showNextQuestion];
}
else{
[self performSegueWithIdentifier:@"incorrect" sender:nil];
}
}
- (IBAction)SelectB:(id)sender {
if ([self.correctAns isEqualToString:@"B"]) {
currentQuestion ++;
[self showNextQuestion];
}
else{
[self performSegueWithIdentifier:@"incorrect" sender:nil];
}
}
- (IBAction)SelectC:(id)sender {
if ([self.correctAns isEqualToString:@"C"]) {
currentQuestion ++;
[self showNextQuestion];
}
else{
[self performSegueWithIdentifier:@"incorrect" sender:nil];
}
}
当我正确回答问题时,它会转到下一个问题。但是,当我回答错误时,它会转到错误的 VC,通知错误的问题。当按下按钮返回问题 VC(我在情节提要中制作)时,此 VC 有一个转场。它确实回到了问题 VC,但只是跳回到了第一个问题。我知道这与 currentQuestion 有关,但不确定是什么。