【问题标题】:How to Pop to a View Controller without Animation如何在没有动画的情况下弹出到视图控制器
【发布时间】:2014-04-07 21:58:07
【问题描述】:

我想显示一个图像以响应在 iPhone 锁定时发生的本地通知事件。当用户在通知上滑动时,我希望我的应用程序通过 popToViewController:animated 转到根视图控制器,然后推送一个显示图像的视图控制器。这在我设置动画 = YES 时有效。当animated = NO 时,显示图像的视图控制器在用户点击后退按钮时不响应。当我 popToViewController 没有动画时,为什么图像视图控制器的导航控件不起作用有什么想法吗?提前致谢。

这是相关代码...

- (void) localNotificationHandler
{
#ifdef kAnimatePop
    animated = YES;    // This works
#else
    animated = NO;     // This doesn't work
#endif
    _showImage = YES;

    // Check if this view controller is not visible
    if (self.navigationController.visibleViewController != self) {
        // Check if there are view controllers on the stack
        if ([self.navigationController.viewControllers count] > 1) {
            // Make this view controller visible
            [self.navigationController popToViewController:self animated:animated];
        }
    }
}    


- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (_showImage) {
        _showImage = NO;
        // Show image in a view controller
        [self performSegueWithIdentifier:@"MainToImageSegue" sender:self];
    }
}

【问题讨论】:

    标签: ios iphone uiviewcontroller uinavigationcontroller poptoviewcontroller


    【解决方案1】:

    在调用 popToViewController 之前不要设置 _showImage。如果它是动画的 viewDidAppear 直到稍后才会被调用,所以它出乎意料地工作。如果它不是动画的,则在设置 _showImage 之前立即调用 viewDidAppear。将_showImage = true; 移动到导航控制器堆栈操作之前。

    【讨论】:

    • 感谢您的回答,但这不是导致问题的原因。我将编辑我的代码以避免混淆。
    【解决方案2】:

    我可以这样检查:

    - (void) localNotificationHandler{
    ...
    
    ...
        if (self.navigationController.topViewController != self) {
            [self.navigationController popToRootViewControllerAnimated:NO];
        }
    }    
    

    我还建议使用类似的东西以编程方式推送您的图像视图控制器

    [self.navigationController pushViewController:<imageVC> animated:YES];
    

    如果您的图像视图控制器的导航按钮不起作用,可能是因为它正在将这些导航消息发送到nil。又名,imageViewControllers self.navigationController 等于 nil。 我不知道如何用故事板解决这个问题,但如果你以编程方式呈现它,这个问题应该会消失。

    【讨论】:

    • 根 VC 可以是 topViewController 但不可见,如果正在显示模态 VC。我不确定手动推送 VC 和使用 segue 方法之间是否有区别。顺便说一句,navigationController 不是零。感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 2012-08-22
    相关资源
    最近更新 更多