1

我有一个带有 3 个视图控制器的故事板QuestionsTableViewControllerQuestionViewControllerAnswerViewController

对于所有密集的目的,QuestionsTableViewController基本上是我的主菜单。it has a selection of topics which when selected populate a question label in QuestionViewControllerwhich is then segued to.

用户在 a 中输入他/她的答案UITextField并点击提交按钮,导致模态 segueAnswerViewController具有标签的标签,该标签基于他们的答案与编码的正确答案的比较向用户返回正确或不正确的消息。这个最终的视图控制器还有一个按钮(返回菜单),单击该按钮应将用户带回QuestionsTableViewController(即我的菜单)。

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

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

我在下面包括我的QuestionViewControllerAnswerViewController课程的片段。

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
4

2 回答 2

1

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

[self.navigationController popViewControllerAnimated:YES];

或者,如果您想转到最顶层的控制器:

[self.navigationController popToRootViewControllerAnimated:YES];
于 2012-08-01T06:10:49.200 回答
0

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

In the prepareForSegue method, you missed this line:

avc.delegate = self;

希望这可以帮助那些陷入我所在的同一个洞的其他人。

于 2012-08-04T01:06:37.773 回答