【问题标题】:After popToRootViewController View is empty, dont know whypopToRootViewController后View为空,不知道为什么
【发布时间】:2014-10-30 11:08:28
【问题描述】:

我正在使用带有不同控制器的UINavigationController。我已经覆盖了导航中的BackButton。在某些情况下,如果点击了BackButton,我想跳转到RootController

但在这种情况下,用户会跳回,但整个 View 是空的。 这种行为很奇怪,我不知道为什么。谁能帮帮我。

要转到RootController,我执行以下操作。

if([self.tempnavigationInfo.refkey isEqual:@"main"]){
    [self.navigationController popToRootViewControllerAnimated:NO];
}

对于BackButton,我已经实现了这个处理程序:

@implementation UINavigationController (ShouldPopOnBackButton)

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {

    if([self.viewControllers count] < [navigationBar.items count]) {
        return YES;
    }

    BOOL shouldPop = YES;
    UIViewController* vc = [self topViewController];
    if([vc respondsToSelector:@selector(navigationShouldPopOnBackButton)]) {
        shouldPop = [vc navigationShouldPopOnBackButton];
    }

    if(shouldPop) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self popViewControllerAnimated:YES];
        });
    } else {
        // Workaround for iOS7.1
        for(UIView *subview in [navigationBar subviews]) {
            if(subview.alpha < 1.) {
                [UIView animateWithDuration:.25 animations:^{
                    subview.alpha = 1.;
                }];
            }
        }
    }

    return NO;
}

在 ViewController 中我是这样处理的:

-(BOOL) navigationShouldPopOnBackButton {
 if (self.backConfirmation != nil
              && self.backConfirmation.navigation != nil
              && self.backConfirmation.confirmation == nil){
        [[NSNotificationCenter defaultCenter] postNotificationName:@"gotoWithoutConfirmation" object:self.backConfirmation.navigation];
        return NO;
    }
    return YES;
}

在我的 MainController 中,我捕获通知并使用上面的“popToRoot”逻辑调用该方法:

-(void) handleNotificationFromFormView:(NSNotification*) notification
{
    if([notification.name isEqualToString:@"gotoWithoutConfirmation"]){
        self.tempnavigationInfo = (NavigationInfo*)notification.object;
        [self goBackTo];
}

【问题讨论】:

  • 请添加更多代码
  • 请告诉我们您是如何返回到其他视图控制器的。您是在从视图中消失的同时删除所有子视图还是其他。
  • 我在帖子中添加了后退按钮逻辑。

标签: ios navigationcontroller


【解决方案1】:

万一其他人在这里结束 - 我有一个类似的问题让我困惑了一段时间 - 原来是因为我使用了一个自定义转换,将根 vc 的内容移出屏幕。在不指定转换以反转翻译的情况下弹出到根目录意味着根目录会在屏幕外显示其内容 - 它不会自动重置为其默认状态。

例如 - 如果您使用 UINavigationControllerDelegate 设置自定义转换,如下所示:

class NavigationControllerDelegate: NSObject, UINavigationControllerDelegate {

 func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {

    if let _ = fromVC as? FirstVC, let _ = toVC as? SecondVC {
      return SlideTransitionAnimator(direction: .left)
    } 
}

您的转换类对 rootViewController 进行了一些转换 - 例如,在我的情况下,将其移出屏幕:

class SlideTransitionAnimator: NSObject, UIViewControllerAnimatedTransitioning {
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

    guard let fromView = transitionContext.view(forKey: .from),
      let toView =  transitionContext.view(forKey: .to) else {return }

    let containerView = transitionContext.containerView
    containerView.addSubview(toView)
    switch direction  {
      case .left:
        toView.frame.origin.x = toView.frame.size.width
      default:
        toView.frame.origin.x = -toView.frame.size.width
    }

    UIView.animate(withDuration: 1,delay: 0, options: .curveEaseInOut,
                   animations: {
                     fromView.alpha = 0
                     switch self.direction  {
                       case .left:
                         fromView.frame.origin.x -= fromView.frame.size.width
                       default:
                         fromView.frame.origin.x += fromView.frame.size.width
                     }
                     toView.alpha = 1.0
                     toView.frame.origin.x = 0
    },
                   completion: { _ in
                     transitionContext.completeTransition(true)
    })

  }
}

如果您在调用 popToRootViewController 时不反转此动画或重置它,您最终可能会得到一个看似空的根视图控制器。

【讨论】:

  • 请提供更多上下文详细说明,是否也可以共享一些代码?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-06
  • 2016-04-11
  • 2013-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多