【问题标题】:popToRootViewControllerAnimated does not display root view controllerpopToRootViewControllerAnimated 不显示根视图控制器
【发布时间】:2012-01-18 19:48:36
【问题描述】:

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

我有一个navigationController,推送了 4 个ViewControllers。我推送的最后一个 vc 以模态方式进一步展示了ViewController。模态ViewController 表示ActionSheet。根据用户的回答,我要么只关闭模式ViewController,要么我想回到根ViewController

在模态呈现的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];

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

作为一种解决方法,我尝试始终关闭模态视图控制器,并在 ViewWillAppear 方法中有 popToRootAnimated 消息。没有不同。仍然会弹出控制器堆栈,但屏幕继续显示我的模态视图控制器的视图,并且继续按顺序执行。

有人可以帮帮我吗?

【问题讨论】:

  • 您是否尝试使用编译器进入 if 循环的断点进行调试?

标签: objective-c ios4 uinavigationcontroller


【解决方案1】:

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

我发现基本上您确实需要关闭该模式视图控制器,但如果您尝试从下一行的导航控制器中弹出,事情就会变得混乱。在尝试弹出之前,您必须确保关闭已完成。在 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:。我在这里的解决方案涉及一个 ivar,可以说:

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
}

【讨论】:

  • 你好。伟大的 !!!它就像一个魅力。非常感谢,我花了 4 多天时间摆弄我的代码。再次感谢。
  • 欢迎来到 stackoverflow.com。乐意效劳。如果这个答案解决了你的问题,你应该接受它。
猜你喜欢
  • 2019-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多