【发布时间】:2013-03-14 22:39:36
【问题描述】:
这个有点困难。 我正在从 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 的问题,但不确定是什么。
【问题讨论】:
-
如果你想回到一个带有segue的控制器,你需要使用一个unwind segue,否则,你将创建一个该控制器的新实例。
-
是的,但是说我在第三个问题上,然后我弄错了,所以当我单击不正确的 VC 上的按钮时,它向我显示了不正确的 VC 它让我回到问题 VC.. .但在第一个问题。那有意义吗?我想让它回到当前问题
-
所有segues(除了展开)总是创建视图控制器的新实例,所以当你“返回”时,你不会回到同一个实例,你正在创建一个新实例,所以它会回到问题1。
-
OKAY...所以关于我如何解决这个问题的任何想法
-
是的,我告诉过你怎么做。使用 unwind segue。你不知道怎么做其中之一吗?
标签: ios xcode loops storyboard viewcontroller