【问题标题】:How to pop from one view controller to another view controller如何从一个视图控制器弹出到另一个视图控制器
【发布时间】:2013-07-25 10:48:21
【问题描述】:

使用 iOS 我现在有 15 个 ViewController,我想从一个 ViewController 弹出到另一个 View Controller。

我正在使用此代码:

SecondViewController *Sec=[SecondViewController alloc]init];
[self.navigationController popViewController:Sec animated:YES];

这显示错误this ViewController not exist,然后我正在使用此代码:

NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:1] animated:YES];

这段代码从thirdViewController 弹出到secondViewController 是正确的。但是当我们从 Ninth(9th)ViewController 弹出到 Fifth(5th)ViewController 然后我在 Ninth(9th)ViewController 中使用这段代码时发生了什么:

NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:4] animated:YES];

它不会从 Ninth(9th)ViewController 弹出到 Fifth(5th)ViewController,而是将 Ninth(9th)ViewController 弹出到 Eight(8th)ViewController。我不知道当我们使用这条线时发生了什么:

NSArray *array = [self.navigationController viewControllers];
NsLog(@"array = %@",array);

当我们在Ninth(9th)ViewController 中使用它时。 NsLog 显示:

array=   First(1st)ViewController;  
         Second(2nd)ViewController;
         Eight(8th)ViewController;
         Ninth(9th)ViewController;

我不知道为什么只显示四个视图控制器。每当我使用 15 个视图控制器时。在每个视图控制器中都会出现此问题。例如,如果我使用 pop form 第十五(15)视图控制器到第五(5)视图控制器,那么同样的问题就会出现。

NSArray *array = [self.navigationController viewControllers];
NsLog(@"array = %@",array);

array=     First(1st)ViewController;  
           Second(2nd)ViewController;
           fourteenth(14th)ViewController;
           fifteenth(15th)ViewController;

我想统计 ViewController 的数量,然后弹出到特定的 ViewController。

【问题讨论】:

  • 你确定所有的 ViewController 都被推送到 NavigationController 中了吗?

标签: iphone ios objective-c uinavigationcontroller uipopovercontroller


【解决方案1】:

您不能弹出到新的视图控制器(就像您使用 secondViewController 示例一样)。

当你使用 UINavigationController 时

将控制器添加到堆栈中:

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

跳到上一个

[self.navigationController popViewControllerAnimated:YES];

弹出到堆栈中的上一个控制器(之前必须已推送):

[self.navigationController popToViewController:<ViewControllerToPopTo> animated:YES];

返回根目录控制器

[self.navigationController popToRootViewControllerAnimated:YES];

【讨论】:

    【解决方案2】:
    for (UIViewController *controller in self.navigationController.viewControllers)
            {
                if ([controller isKindOfClass:[nameOfYourViewControllerYouWantToNavigate class]])
                {
                    [self.navigationController popToViewController:controller animated:YES];
    
                    break;
                }
            }
    

    【讨论】:

      【解决方案3】:

      Swift 4.0 - Swift 5.0

       for controller in self.navigationController!.viewControllers as Array {
                  if controller.isKind(of: HomeViewController.self) {
                      self.navigationController!.popToViewController(controller, animated: true)
                      break
                  }
              }
      

      【讨论】:

        【解决方案4】:

        试试这个

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

        【讨论】:

          【解决方案5】:

          第一:

           SecondViewController *Sec=[SecondViewController alloc]init];
           [self.navigationController popViewController:Sec animated:YES];
          

          您不能这样做,因为您分配了一个不在导航控制器中的新 Sec 视图控制器。

          考虑使用这个:

          你在 9 视图控制器中

          for (int i= 0 ; i < [[self.navigationController viewControllers]count] ; i++) {
              if ( [[[self.navigationController viewControllers] objectAtIndex:i] isKindOfClass:[FifiViewControllerClassname class]]) {
                  [self.navigationController popToViewController:[array objectAtIndex:i] animated:YES];
              }
          }
          

          【讨论】:

            【解决方案6】:

            这样试试

            MyTableViewController *vc = [[MyTableViewController alloc] init];
            NSMutableArray *controllers = [NSMutableArray    
            arrayWithArray:self.navigationController.viewControllers];
            [controllers removeLastObject];
            [controllers addObject:vc]; 
            

            【讨论】:

              【解决方案7】:
              BOOL check = FALSE;
              NSArray *viewControllers = [[self navigationController] viewControllers];
              id obj;
              for( int i=0;i<[viewControllers count];i++)
              {
                  obj=[viewControllers objectAtIndex:i];
                  if([obj isKindOfClass:[yourclassname class]])
                  {
                      check = TRUE;
                      break;
                  }
              }
              
              if (check)
              {
              
                  [[self navigationController] popToViewController:obj animated:YES];
              }
              else
              {
                  yourclassname *yourclassnameObj=[self.storyboard instantiateViewControllerWithIdentifier:@"yourclassStoryBoardID"];
                  [self.navigationController pushViewController:yourclassnameObj animated:true];
              
              }
              

              【讨论】:

                【解决方案8】:

                对于 Swift 3.0,使用过滤器:

                let desiredViewController = self.navigationController!.viewControllers.filter { $0 is YourViewController }.first!
                self.navigationController!.popToViewController(desiredViewController, animated: true)
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2020-10-04
                  • 1970-01-01
                  • 1970-01-01
                  • 2019-07-03
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2017-04-22
                  相关资源
                  最近更新 更多