Reputation: 2113
I have the need to pop back a number of views but not all the way back to the root controller, I cant find a way other than going all the way back using the following
[self.navigationController popToRootViewControllerAnimated:YES];
how do I pop back to root + 1 ?
Upvotes: 0
Views: 99
Reputation: 1000
I'm using following methods to pop to specific VC:
-(void)popToViewControllerWithAnimation:(BOOL)showAnim WithClass:(Class)class{
for(UIViewController *vc in self.navigationController.viewControllers){
if([vc isKindOfClass:class]){
[self.navigationController popToViewController:vc animated:showAnim];
break;
}
}
}
-(void)popViewControllers:(int)count{
[self.navigationController popToViewController: self.navigationController.viewControllers[self.navigationController.viewControllers.count-count-1] animated:YES];
}
Upvotes: 0
Reputation: 124
if you kown what the viewcontroller you want to back.
UIViewController *targetVC = XXViewController.class ;
NSArray <UIViewController *>*vcs = self.navigationController.viewControllers ;
[vcs enumerateObjectsUsingBlock:^(UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:targetVC.class]) {
[self.navigationController popToViewController:obj animated:YES];
*stop = YES ;
}
}];
if you kown what the index you want to back.
NSInteger index = 1 ;
[self.navigationController popToViewController:vcs[index] animated:YES];
follow is unimportant
current you have AA,BB two viewcontroller in you navigation . you goto the DD viewcotrlooer and want't show the last viewcontroller when back. when you pop the DD viewcotrller you can use the follow code. The most important thing is to support sideslip.
[self skipCurrentViewcontrollerToViewcontroller:DD.new];
- (void)skipCurrentViewcontrollerToViewcontroller:(UIViewController *)toVC
{
if (self.navigationController == nil) {
NSAssert(NO, @"the navigation is empty!");
return ;
}
NSArray *vcArray = self.navigationController.viewControllers ;
if (vcArray.count > 0) {
NSMutableArray *desArray = [NSMutableArray arrayWithCapacity:4];
for (int i = 0 ;i < vcArray.count - 1 ; i++) {
[desArray addObject:vcArray[i]];
}
[desArray addObject:toVC];
[self.navigationController setViewControllers:desArray animated:YES];
}
else{
[self.navigationController pushViewController:toVC animated:YES];
}
}
Upvotes: 1
Reputation: 6524
int viewControllerIndex = 1; //Note that object 1 is the first object after the root
NSArray *viewControllersArray = self.navigationController.viewControllers ;
[self.navigationController popToViewController:viewControllersArray[viewControllerIndex] animated:YES];
Upvotes: 0