如何弹出回到rootViewController +1?

时间:2018-08-29 13:11:54

标签: objective-c uiviewcontroller

我需要弹出一些视图,但不是一直返回到根控制器,除了找到以下内容,我找不到其他方法

[self.navigationController popToRootViewControllerAnimated:YES];

如何弹出根目录+ 1?

3 个答案:

答案 0 :(得分:1)

如果知道要返回的视图控制器,那么<​​/ p>

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

如果您知道要支持什么索引。

 NSInteger index = 1 ;
[self.navigationController popToViewController:vcs[index] animated:YES];

关注并不重要

当前您的导航中有AA,BB两个视图控制器。您转到DD viewcotrlooer,并且不想在返回时显示最后一个viewcontroller。当您弹出DD Viewcotrller时,可以使用以下代码。最重要的是要支撑侧滑。

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我正在使用以下方法弹出特定的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];
}