美文网首页
iOS pushViewController 有坑

iOS pushViewController 有坑

作者: 伊卡洛斯_路西法 | 来源:发表于2017-04-01 14:33 被阅读617次

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

先上代码

[self.navigationController pushViewController:tempVC animated:YES];
NSMutableArray * subVCs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
        for (int i=0; i<subVCs.count; i++) {
            if ([subVCs[i] isKindOfClass:[XXX class]]) {
                [subVCs removeObjectAtIndex:i];
            }
        }
        [self.navigationController setViewControllers:subVCs animated:YES];```

大家一般都是这样做,可是,但是,pushViewController执行之后,有一定概率self.navigationController.viewControllers没有你刚才push的vc,这就是坑,它不是立马入栈的。
***
#解决方法,很简单
* 设置代理
`self.navigationController.delegate = self;`
* 实现代理函数
```NSMutableArray * subVCs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
        for (int i=0; i<subVCs.count; i++) {
            if ([subVCs[i] isKindOfClass:[XXX class]]) {
                [subVCs removeObjectAtIndex:i];
            }
        }
        [self.navigationController setViewControllers:subVCs animated:YES];```
嗯,是不是很简单。

相关文章

网友评论

      本文标题:iOS pushViewController 有坑

      本文链接:https://www.haomeiwen.com/subject/nyuiottx.html