popToRootViewControllerAnimated不显示根视图控制器 [英] popToRootViewControllerAnimated does not display root view controller

查看:226
本文介绍了popToRootViewControllerAnimated不显示根视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些关于导航控制器问题的帮助。

I need a little help on a problem with navigation controllers.

我有一个 navigationController with 4 ViewControllers 推送。我推送的最后一个vc以模态方式呈现了另一个 ViewController 。模态 ViewController 提供 ActionSheet 。根据用户的答案,我要么只关闭模态 ViewController ,要么我想回到根 ViewController

I have a navigationController with 4 ViewControllers pushed. The last vc I push presents a further ViewController modally. The modal ViewController presents an ActionSheet. Depending on the user's answer I either dismiss the modal ViewController only or I want to go back to the root ViewController.

在模拟的 ViewController 中我有:

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

在最后 ViewController 推入 navigationController 堆栈我有:

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

解雇模态视图控制器工作正常。
但是,

Dismissing the modal view controller works fine. However,

[self.navigationController popToRootViewControllerAnimated:NO];

不会导致根 ViewController 显示它的观点。添加一些日志信息我看到在消息到 self.navigationController 之后,堆栈被正确弹出,但执行依次继续。屏幕仍然显示模态ViewController的视图。

does not cause the root ViewController to display its views. Adding some log info I see that after the message to self.navigationController the stack is correctly popped but execution continues sequentially. The screen still shows the view of the modal ViewController.

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

As a workaround I tried always dismissing the modal view controller and in the ViewWillAppear method have the popToRootAnimated message. No difference. Still the stack of controllers is popped but the screen continues showing my modal view controller's view and execution continues sequentially.

有人可以帮我吗?

推荐答案

我喜欢这些欺骗性的问题。看起来很简单,直到你尝试这样做。

I like these deceptive questions. It seems very simple, until you try to do it.

我发现基本上你需要解雇那个模态视图控制器,但如果你试图从下一行的导航控制器事情搞得一团糟。在尝试弹出之前,您必须确保完成解散。在iOS 5中,您可以使用 dismissViewControllerAnimated:completion:之类的。

What I found was that basically you do need to dismiss that modal view controller, but if you try to pop from the navigation controller on the next line things get mixed up. You must ensure the dismiss is complete before attempting the pop. In iOS 5 you can use dismissViewControllerAnimated:completion: like so.

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

但我看到你的问号标签中有4.0。我找到的< iOS 5 的解决方案远不那么漂亮,但应该仍然有用,听起来你已经走在了路上。你想 viewDidAppear:不是 viewWillAppear:。我的解决方案涉及一个ivar,让我们说:

But I see you have 4.0 in your question tags. The solution I found for <iOS 5 is far less pretty but should still work, and it sounds like you were already on the trail. You want viewDidAppear: not viewWillAppear:. My solution here involves an ivar, lets say:

BOOL shouldPopToRootOnAppear;

然后你的 GameReportModalWillBeDismissed:会看起来像什么像这样:

And then your GameReportModalWillBeDismissed: would look something like this:

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

您的 viewDidAppear:看起来像这样......

And your viewDidAppear: would look like this...

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

这篇关于popToRootViewControllerAnimated不显示根视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆