如何从导航控制器ios中删除以前的视图控制器?

问题描述:

我想从导航堆栈中删除以前的视图控制器。如何从导航控制器ios中删除以前的视图控制器?

例如

- > A是根view.now导航到B - B导航到c - c浏览到c ​​- c浏览到c

- >现在我想删除所有的C视图控制器&弹出到B

B未修正视图控制器。

又如

甲 - >乙 - > C>克> C>˚F> C> C> C> c^

取下导航&需要所有C查看控制器以下输出

A - >乙 - > C> g>的C>˚F

请帮我

+1

这可能会帮助你:https://stackoverflow.com/a/10281744/4763963 –

+0

我已经检查this.but不工作 –

+0

我不能理解的是,你如何能够从c导航到c 。 > A - > b - > c> g> c> f> c> c> c> c – Prashant

您可以使用popToViewControllerhttps://developer.apple.com/documentation/uikit/uinavigationcontroller/1621871-poptoviewcontroller)。

例如:

在目标C

[self.navigationController popToViewController:self.navigationController.viewControllers[your_VC_index] animated: true]; 
// in first eg your_VC_index will be 1 and another example it will be 5 

在夫特3

self.navigationController.popToViewController(self.navigationController.viewControllers[your_VC_index], animated: true) 

确定从导航堆栈控制器。和POP到控制器

NSArray *viewControllers = [[self navigationController] viewControllers]; 
    int count = [viewControllers count]; 

    for (int i= count-2; i >= 0 ; i--) { 

     id obj=[viewControllers objectAtIndex:i]; 

     if(![obj isKindOfClass:[C class]]){ 
      [[self navigationController] popToViewController:obj animated:YES]; 
      return; 
     } 
    } 
+0

通过在导航堆栈中传递上面的所有视图控制器,它将转到第一个不匹配的视图控制器。 – suhit

+0

是的,那是他想要的。检查他的例子@suhit – Subramanian

+0

导航堆栈是'A-> b> c> c> c> c> c> c> c> c'需要的输出是'A - > b - > c> g> c> f'您的解决方案将显示ViewController-A,因为它将停止在第一个索引本身。 – suhit

反向叠代导航堆栈并获得第一个非匹配的viewController的索引,然后popToViewController

if let controllers = self.navigationController?.viewControllers { 
    var count = controllers.count - 1 
    for viewcontroller in controllers.reversed() { 
     if viewcontroller is CViewController { 
      count -= 1 
     } else { 
      break 
     } 
    } 

    let vc = controllers[count] 
    self.navigationController?.popToViewController(vc, animated: true) 
} 

写方法类似于以下,

- (void)removeAllLastCObjectsFromNavigationController { 
    //fetch viewcontroller array. for your case which will be A - > b -> c > g > c > f > c > c > c > c 
    NSArray *viewControllers = yourNavigationController.viewControllers; 
    NSMutableArray *mutableArray = [NSMutableArray arrayWithArray: viewControllers]; 

    while(1) { 
     id viewController = mutableArray.lastObject; 
     if ([viewController isKindOfClass:[YourCClassName class]]) { 
      //Now continue removing the objects from the array if that is C type object. 
      [mutableArray removeLastObject]; 
     } else { 
      //If found any other class than C stop removing viewcontroller 
      break; 
     } 
    } 
    //Now mutableArray contains A - > b -> c > g > c > f 
    yourNavigationController.viewControllers = mutableArray; 
} 

由于navigationController的childViewControllers是一个堆栈,
所以使用此功能,您可以弹出到f viewController和“c> c> c> c”被销毁。

for (UIViewController *tempVC in self.navigationController.childViewControllers) { 
    if ([tempVC isKindOfClass:[fViewController class]]) { 
     fViewController *f = (fViewController *)tempVC; 
     [self.navigationController popToViewController:f animated:YES]; 
    } 
} 

但是,如果你不想弹出到f,只想获取navigationController的新的childViewControllers。您可以使用下一个功能:

for (UIViewController *tempVC in self.navigationController.childViewControllers) { 
    if ([tempVC isKindOfClass:[ScanViewController class]]) { 
     NSUInteger findex = [self.navigationController.childViewControllers indexOfObject:tempVC]; 
     NSMutableArray *viewArray = [NSMutableArray arrayWithArray:self.navigationController.childViewControllers]; 
     [viewArray removeObjectsInRange:NSMakeRange(findex, viewArray.count - 1)]; 
    } 
} 

的viewArray是新childViewControllers.And如果你有新的需求,可以编辑该功能,然后使用它。