JRulle
JRulle

Reputation: 7568

How to dismissing multiple view controllers?

I have a storyboard with 3 view controllers: QuestionsTableViewController, QuestionViewController and AnswerViewController

For all intensive purposes, QuestionsTableViewController is basically my main menu. it has a selection of topics which when selected populate a question label in QuestionViewController which is then segued to.

The user enters his/her answer in a UITextField and hits a submit button causing a modal segue to the AnswerViewController which has a label that returns either a correct or incorrect message to the user based on a comparison of their answer to the coded correct answer. This final View Controller also has a button (back to menu) that when clicked should take the user back to the QuestionsTableViewController (i.e. my menu).

This last part (the returning to the menu) is where I am having problems.

I can dismiss the AnswerViewController multiple ways, but I cant figure out what I need to do to dismiss the QuestionViewController as part of this same button press.

I am including snipets of my QuestionViewController and AnswerViewController classes below.

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

Upvotes: 1

Views: 518

Answers (2)

JRulle
JRulle

Reputation: 7568

in an effort to complete this post for others who may reference it, i am reposting user523234's answer here:

In the prepareForSegue method, you missed this line:

avc.delegate = self;

hopefully this can help someone else who has fallen into the same hole that i was in.

Upvotes: 0

Khurram Ali
Khurram Ali

Reputation: 835

When you dismiss the modal view, in the completion block have this code:

[self.navigationController popViewControllerAnimated:YES];

Or if you want to go to the top most controller:

[self.navigationController popToRootViewControllerAnimated:YES];

Upvotes: 1

Related Questions