0

I want to remove Previous view controller from navigation stack.

For example

-> A is root view.now navigate to B - b navigate to c - c navigate to c - c navigate to c

--> now i want to remove all c view controller & pop to B

B is not Fix view controller.

Another example

A - > b -> c > g > c > f > c > c > c > c

Remove all c View controller from navigation & need following output

A - > b -> c > g > c > f

Please help me

Maulik shah
  • 1,664
  • 1
  • 19
  • 45

5 Answers5

2

Identify the controller from navigation stack. And pop to that controller

  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;
        }
   }
Subramanian P
  • 4,365
  • 2
  • 21
  • 25
0

You can use popToViewController (https://developer.apple.com/documentation/uikit/uinavigationcontroller/1621871-poptoviewcontroller).

example:

In Objective 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

In Swift 3

self.navigationController.popToViewController( self.navigationController.viewControllers[your_VC_index], animated: true)
Venk
  • 5,949
  • 9
  • 41
  • 52
0

reverse iterate over navigation stack and get the index of first non matching viewController and then 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)
 }
Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
0

Write a method something like the following,

- (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;
}
shuvo
  • 705
  • 5
  • 15
0

Because the childViewControllers of navigationController is a stack,
So use this function, you can pop to f viewController and the " c > c > c > c" was destroyed.

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

However, if you don't want to pop to f, just want to get the new childViewControllers of navigationController. you can use next functions:

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)];
    }
}

The viewArray is the new childViewControllers.And if you have new demand, can edit this function then use it.

Xinboy
  • 71
  • 6