iOS pop到没有入栈的控制器

话不多说,直接上代码

    BOOL isExistVC = NO;

    UINavigationController *navVC = self.navigationController;

    NSMutableArray *viewControllers = [[NSMutableArray alloc] init];

    for (UIViewController *vc in [navVC viewControllers]) {

        [viewControllers addObject:vc];

        if ([vc isKindOfClass:[OpenConfirmInfoVC class]]) {

            isExistVC = YES;

            break;

        }

    }

    if (isExistVC) {

        [self.navigationController popViewControllerAnimated:YES];

    }else{

        OpenConfirmInfoVC *OpenConVC = [[OpenConfirmInfoVC alloc] init];

/* 获取当前路由的控制器数组*/

        NSMutableArray *vcArray = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];


/* 打印当前路由的控制器数组*/

        NSLog(@"==the vcArray is %@", vcArray);


/* 获取当前控制器在路由的位置*/

        int index = (int)[vcArray indexOfObject:self];


/* 移除当前路由器*/

        [vcArray removeObjectAtIndex:index];


/* 添加新控制器*/

        [vcArray addObject: OpenConVC];


/* 打印新增后的控制器数组*/

        NSLog(@"==the vcArray is %@", vcArray);


        /* 重新设置当前导航控制器的路由数组 */

        [self.navigationController setViewControllers:vcArray animated:YES];


    }

你可能感兴趣的:(iOS pop到没有入栈的控制器)