如何解雇多个视图控制器?

时间:2012-08-01 04:08:48

标签: objective-c ios uiviewcontroller

我有一个包含3个视图控制器的故事板:QuestionsTableViewControllerQuestionViewControllerAnswerViewController

出于所有密集目的,QuestionsTableViewController基本上是我的主菜单。它有一系列主题,选中时会在QuestionViewController中填充一个问题标签,然后进行搜索。

用户在UITextField中输入他/她的答案并点击提交按钮,导致模式segue AnswerViewController,其标签返回正确或基于他们对编码的正确答案的答案的比较,向用户提供不正确的消息。此最终视图控制器还有一个按钮(返回菜单),点击该按钮后,用户将返回QuestionsTableViewController(即我的菜单)。

最后一部分(返回菜单)是我遇到问题的地方。

我可以忽略AnswerViewController多种方式,但我无法弄清楚我需要做什么来解除QuestionViewController作为同一按钮按下的一部分。

我在下面列出了QuestionViewControllerAnswerViewController类的snipets。

QuestionViewController.m

#import "QuestionViewController.h"
#import "AnswerViewController.h"

@interface QuestionViewController ()

@end

@implementation QuestionViewController

@synthesize currentQuestionDisplay;
@synthesize userAnswerTextField;
@synthesize currentQuestion;

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    AnswerViewController *avc = [segue destinationViewController];
    [avc setCurrentQuestion:currentQuestion];
    [avc setUserAnswer:[userAnswerTextField text]];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.currentQuestionDisplay setText:[currentQuestion question]];

    // Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [self setCurrentQuestionDisplay:nil];
    [self setUserAnswerTextField:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)dismissKeyboard:(id)sender {
    [userAnswerTextField resignFirstResponder];
}

- (void)dimissThisVC
{
    [self dismissViewControllerAnimated:YES completion:^(void){}];
}

@end


AnswerViewController.m

#import "AnswerViewController.h"
#import "QuestionViewController.h"

@interface AnswerViewController ()

@end

@implementation AnswerViewController

@synthesize displayCurrentAnswer;
@synthesize currentQuestion;
@synthesize userAnswer;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    if([userAnswer isEqualToString:currentQuestion.answer]) {
        [self.displayCurrentAnswer setText:@"You are correct!"];
    }
    else {
        [self.displayCurrentAnswer setText:@"You are wrong!"];
    }

    // Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [self setDisplayCurrentAnswer:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)dismissAnswerVC:(id)sender {
    [[self presentingViewController]
           dismissViewControllerAnimated:YES
           completion:^(void){ }];
}

@end

2 个答案:

答案 0 :(得分:1)

当您关闭模态视图时,在完成块中有以下代码:

[self.navigationController popViewControllerAnimated:YES];

或者,如果你想进入最顶级的控制器:

[self.navigationController popToRootViewControllerAnimated:YES];

答案 1 :(得分:0)

为了能够为其他可能引用它的人完成这篇文章,我在这里重新发布user523234的答案:

In the prepareForSegue method, you missed this line:

avc.delegate = self;
希望这可以帮助那些陷入同一个洞的人。