2012-01-18 88 views
5

我需要一些关于导航控制器问题的帮助。我有一个navigationController与4 ViewControllers推。我推的最后一个vc以模态方式呈现另一个ViewController。模态ViewController呈现ActionSheet。根据用户的回答,我要么仅仅关闭模式ViewController,要么我想回到根目录ViewControllerpopToRootViewControllerAnimated不显示根视图控制器

中提出的ViewController模态,我有:

- (void) dismissGameReport 
{  
    [[self delegate] GameReportModalWillBeDismissed:modalToPopToRoot];  
} 

在过去ViewController压入堆栈navigationController我:

- (void)GameReportModalWillBeDismissed: (BOOL)popToRoot; 
{  
    if (popToRoot) 
     { 
     [self.navigationController popToRootViewControllerAnimated:NO]; 
     } 
    else 
     { 
     [self dismissModalViewControllerAnimated:YES]; 
     }    
} 

解雇模态视图控制器正常工作。 然而,

[self.navigationController popToRootViewControllerAnimated:NO]; 

不会引起根0​​以显示其意见。添加一些日志信息我看到消息到self.navigationController后,堆栈被正确弹出,但执行顺序继续。屏幕仍然显示模态ViewController的视图。

作为解决方法,我总是试图解散模态视图控制器,并在ViewWillAppear方法中有popToRootAnimated消息。没有不同。仍然弹出一堆控制器,但屏幕继续显示我的模态视图控制器的视图,并继续执行。

有人可以帮我吗?

+0

你有没有尝试用断点调试你的编译器进入循环? – Hiren 2012-01-19 13:03:41

回答

6

我喜欢这些欺骗性的问题。这似乎很简单,直到你尝试去做。

我发现基本上你需要关闭那个模态视图控制器,但是如果你尝试从下一行的导航控制器弹出,事情会变得混乱起来。在尝试弹出之前,您必须确保解散完成。在iOS 5中,您可以像这样使用dismissViewControllerAnimated:completion:

-(void)GameReportModalWillBeDismissed:(BOOL)popToRoot{  
    if (popToRoot){ 
     [self dismissViewControllerAnimated:YES completion:^{ 
      [self.navigationController popToRootViewControllerAnimated:YES]; 
     }]; 
    } 
    else{ 
     [self dismissModalViewControllerAnimated:YES]; 
    }    
} 

但我看到你的问题标签有4.0。我找到的<iOS 5的解决方案不太漂亮,但应该仍然可以工作,而且听起来你已经在路上了。你想viewDidAppear:不是viewWillAppear:。在这里,我的解决方案包括伊娃,让说:

BOOL shouldPopToRootOnAppear; 

然后你GameReportModalWillBeDismissed:会是这个样子:

-(void)GameReportModalWillBeDismissed:(BOOL)popToRoot{  
    shouldPopToRootOnAppear = popToRoot; 
    [self dismissModalViewControllerAnimated:YES];   
} 

和你viewDidAppear:应该是这样的......

-(void)viewDidAppear:(BOOL)animated{ 
    [super viewDidAppear:animated]; 
    if (shouldPopToRootOnAppear){ 
     [self.navigationController popToRootViewControllerAnimated:YES]; 
     return; 
    } 
    // Normal viewDidAppear: stuff here 
} 
+0

你好。太好了!它像一个魅力。非常感谢,这是超过4天我正在摆弄我的代码。再次感谢。 – DanL 2012-01-19 20:02:50

+0

欢迎来到stackoverflow.com。乐意效劳。如果这个答案解决了你的问题,你应该接受它。 – NJones 2012-01-19 21:23:25

相关问题