如何从一个视图控制器popup到另一个视图控制器

使用iOS我现在有15个ViewController我想从一个ViewControllerpopup到另一个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]; 

这个代码是正确的从thirdViewControllerpopup到secondViewController。 但是当我们从第九(第九)ViewControllerpopup到第五(第五)ViewController时发生了什么,然后我在第九(第九)ViewController中使用这个代码:

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

它不会从第九(9)视图控制器到第五(第五)视图控制器,除了它将第九(9)视图控制器popup到八(8)视图控制器。 当我们使用这一行时,我不知道发生了什么事情:

 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个视图控制器。 每个视图控制器中会发生此问题。 例如,如果我使用popup窗体第十五(第十五)ViewController到第五(第五)ViewController然后同样的问题体现。

 NSArray *array = [self.navigationController viewControllers]; NsLog(@"array = %@",array); array= First(1st)ViewController; Second(2nd)ViewController; fourteenth(14th)ViewController; fifteenth(15th)ViewController; 

我想计数ViewController的数量,然后popup到特定的ViewController。

 for (UIViewController *controller in self.navigationController.viewControllers)    {      if ([controller isKindOfClass:[nameOfYourViewControllerYouWantToNavigate class]])      {        [self.navigationController popToViewController:controller animated:YES];        break;      }    } 

你不能popup一个新的视图控制器(就像你用你的secondViewController的例子)。

当你使用UINavigationController的时候

添加控制器到堆栈:

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

popup到以前的一个

 [self.navigationController popViewControllerAnimated:YES]; 

popup到堆栈中的前一个控制器 (必须在之前被推送):

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

回到根控制器

 [self.navigationController popToRootViewControllerAnimated:YES]; 

尝试这个

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

第一:

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

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

考虑使用这个:

你在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]; } } 

像这样尝试

 MyTableViewController *vc = [[MyTableViewController alloc] init]; NSMutableArray *controllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers]; [controllers removeLastObject]; [controllers addObject:vc]; 
 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]; } 

对于Swift 3.0 ,使用filter:

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