【问题标题】:'Tried to pop to a view controller that doesn't exist.'“试图弹出一个不存在的视图控制器。”
【发布时间】:2011-01-11 21:20:52
【问题描述】:

我在调用方法dismissView 时收到此错误。这是方法存根:

-(IBAction)dismissView
{
    RootViewController *rootController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
    [self.navigationController popToViewController:rootController animated:YES];
}

这应该可行,而且我已经检查过,rootController 已初始化并分配。有什么想法吗?

【问题讨论】:

    标签: iphone objective-c uiviewcontroller uinavigationcontroller


    【解决方案1】:

    我最近遇到了这个问题,并用类似的方法解决了......

    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
    

    【讨论】:

    • swift 3 实现let _ = self.navigationController?.popToViewController((self.navigationController?.viewControllers[1]) as! HomeViewController, animated: true)
    【解决方案2】:

    -popToViewController 用于将视图控制器从堆栈中弹出,直至已存在的。你的 UINavigationController 有一堆 ViewControllers(存储在 viewControllers 属性中),当你 popToViewController 时,你会想要传递该数组中的一个元素作为第一个参数。

    在这种情况下,您最可能想要做的是使用 -popViewControllerAnimated:,这将从堆栈中删除顶部的 ViewController

    【讨论】:

      【解决方案3】:

      斯威夫特 4

      对于仍在寻找不涉及 UINavigationController 堆栈索引的更好解决方案的任何人,更大的导航堆栈会带来更多问题 - 这是解决此问题的最简单方法:

      if let destinationViewController = navigationController?.viewControllers
                                                              .filter(
                                            {$0 is DestinationViewController})
                                                              .first {
          navigationController?.popToViewController(destinationViewController, animated: true)
      }
      

      【讨论】:

      • 为什么不使用$0 is DestinationViewController 而不是$0.classForCoder == DestinationViewController.self
      【解决方案4】:

      您正在那里分配 RootViewController。它不存在于导航控制器的堆栈中,因此无论弹出多远,都无法到达。

      【讨论】:

        【解决方案5】:

        如果您使用的是故事板,请使用此转场:

        #import "PopToControllerSegue.h"
        
        @implementation PopToControllerSegue
        
        - (void) perform
        {
            UIViewController *sourceViewController = (UIViewController *)self.sourceViewController;
            UIViewController *destinationViewController = (UIViewController *)self.destinationViewController;
        
            for (UIViewController* controller in sourceViewController.navigationController.viewControllers) {
                if ([controller isKindOfClass:destinationViewController.class]) {
                    [sourceViewController.navigationController popToViewController:controller animated:YES];
                    return;
                }
            }
        
            NSLog(@"PopToControllerSegue has failed!");
        }
        
        @end
        

        【讨论】:

          【解决方案6】:

          使用 Push Segues 时,您可以使用此方法轻松返回根目录:

          [self.navigationController popToRootViewControllerAnimated:YES];
          

          使用 Modal Segues 时(因为问题中的 dismiss 一词并作为一般参考),您可以使用此方法 dismiss 视图控制器:

          [self dismissViewControllerAnimated:YES completion:nil];
          

          【讨论】:

            【解决方案7】:

            UINavigationController 有一个 ViewControllers 堆栈,存储在 viewControllers(NSArray) 属性中。枚举到所需的ViewController 并弹出到该ViewController

            以下代码应该可以解决问题。

            -(IBAction)dismissView
            {
                NSArray *array = self.navigationController.viewControllers;
                for (id controller in array) {
                    if ([controller isKindOfClass:[RootViewController class]]) {
                        [self.navigationController popToViewController:controller animated:YES];
            
                    }
                }
            }
            

            【讨论】:

              【解决方案8】:

              试试这个单行解决方案。

              Swift 4+:

              self.navigationController?.popToViewController ((self.navigationController?.viewControllers[1]) as! Your_ViewController, animated: true)
              

              目标-C:

              [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
              

              【讨论】:

                【解决方案9】:

                在我的情况下,问题是我在根 ViewController 中,这仅适用于堆叠在它上面的 VC。 要弹出到 RootViewController 使用

                navigationController?.popToRootViewController(animated: true)
                

                【讨论】:

                  【解决方案10】:
                  self.navigationController?.present(viewControllers, animated: true, completion: nil)
                  

                  【讨论】:

                    猜你喜欢
                    • 2014-02-04
                    • 1970-01-01
                    • 1970-01-01
                    • 2011-01-28
                    • 1970-01-01
                    • 1970-01-01
                    • 2020-10-04
                    • 2013-07-25
                    • 1970-01-01
                    相关资源
                    最近更新 更多