2012-08-01 57 views
1

我有3視圖控制器故事板:QuestionsTableViewControllerQuestionViewControllerAnswerViewController如何解散多個視圖控制器?

對於所有密集的目的,QuestionsTableViewController基本上是我主菜單。它有一個選擇的主題,當選擇填充QuestionViewController問題標籤,然後被挑選。

用戶輸入的UITextField他/她的答案,並點擊提交按鈕造成模態SEGUEAnswerViewController其中有一個返回無論是正確或不正確的消息,根據他們的答案進行比較的用戶標籤到編碼正確的答案。這個最終視圖控制器也有一個按鈕(回到菜單),點擊時,應將用戶帶回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 
+0

我沒有與故事板的經驗,但看到這一點:。http://stackoverflow.com/a/11381182/1430069 雖然這並沒有提到問題中的故事板,但我猜'view controller'的行爲是相同的。 – 2012-08-01 04:29:21

回答

1

當您關閉模式的看法,在完成塊有這樣的代碼:

[self.navigationController popViewControllerAnimated:YES]; 

或者,如果你想要去到最高控制器:

[self.navigationController popToRootViewControllerAnimated:YES]; 
+0

不幸的是,這還沒有爲我工作......我一直在研究這個問題的核心,我想我可能已經找到了一個解決方案,結合你的上述想法和一點委託。我會在今天晚上嘗試併發布我的結果。手指越過,我們終於可以得到答案。 – JRulle 2012-08-03 12:23:59

+0

因爲我找不到工作答案,但我確實縮小了問題的範圍,在我的代表團設置中,我在這裏發佈了一個新問題:http://stackoverflow.com/questions/11804472/delegation-issue-with -dismissing-多視圖控制器 – JRulle 2012-08-03 23:16:44

0

努力完成這篇文章,爲其他人誰可以參考它,我要重新發布user523234的答案在這裏:

In the prepareForSegue method, you missed this line: 

avc.delegate = self; 

希望這可以幫助別人誰已陷入同樣的​​孔,我是在