背景:在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<subVCs.count; i++) {
if ([subVCs[i] isKindOfClass:[XXX class]]) {
[subVCs removeObjectAtIndex:i];
}
}
[self.navigationController setViewControllers:subVCs animated:YES];
大家一般都是這樣做,可是,但是,pushViewController執(zhí)行之后,有一定概率self.navigationController.viewControllers沒有你剛才push的vc,這就是坑,它不是立馬入棧的。
解決方法,很簡單
- 設置代理
self.navigationController.delegate = self; - 實現(xiàn)代理函數(shù)
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];
嗯,是不是很簡單。