Maulik shah
Maulik shah

Reputation: 1704

How can I remove Previous View Controller from navigation controller ios?

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

Upvotes: 0

Views: 3469

Answers (5)

Xinboy
Xinboy

Reputation: 71

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.

Upvotes: 1

shuvo
shuvo

Reputation: 705

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

Upvotes: 0

Subramanian P
Subramanian P

Reputation: 4375

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

Upvotes: 2

Suhit Patil
Suhit Patil

Reputation: 12023

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

Upvotes: 0

Venk
Venk

Reputation: 5955

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)

Upvotes: 0

Related Questions