iOS pushViewController 有坑

背景:在pushViewController之后把之前的vc删掉

先上代码

UIViewController *tempVC = [[UIViewController alloc] init];
[self.navigationController pushViewController:tempVC animated:YES];
NSMutableArray * subVCs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
        for (int i=0; i

大家一般都是这样做,可是,但是,pushViewController执行之后,有一定概率self.navigationController.viewControllers没有你刚才push的vc,这就是坑,它不是立马入栈的。


解决方法,很简单

  • 设置代理
    self.navigationController.delegate = self;
  • 实现代理函数
NSMutableArray * subVCs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
        for (int i=0; i

嗯,是不是很简单。

你可能感兴趣的:(iOS pushViewController 有坑)