1

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
JRulle
  • 7,448
  • 6
  • 39
  • 61
  • I do not have experience with storyboard but see this: http://stackoverflow.com/a/11381182/1430069 Although this does not mention storyboard in the question but I guess the behavior of `view controller` is same either way. – Sehrish Khan Aug 01 '12 at 04:29

2 Answers2

1

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];
Khurram Ali
  • 835
  • 12
  • 18
  • unfortunately, this hasnt worked for me yet... I have been researching the issue hardcore and I think I may have found a solution that combines your above idea with a bit of delegation. I will try it this evening and post my results. Fingers crossed we may finally have an answer. – JRulle Aug 03 '12 at 12:23
  • Since i could not find a working answer, but I did narrow down the issue to something in my delegation set-up, i posted a new question here: http://stackoverflow.com/questions/11804472/delegation-issue-with-dismissing-multiple-view-controllers – JRulle Aug 03 '12 at 23:16
0

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.

JRulle
  • 7,448
  • 6
  • 39
  • 61