iOS UINavigationController 替换当前显示的ViewController,即先pop,立即再push

需求如标题所示,但不能像下面的写法:

   [self.navigationController popViewControllerAnimated:NO];
   [self.navigationController pushViewController:self.toViewController animated:YES];


    因为这样会看到上层的ViewController闪一下,完美的解决方法如下所示:


    NSMutableArray *viewCtrs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    NSInteger index = [viewCtrs indexOfObject:self];
    NSInteger i = viewCtrs.count;
    i--;
    for(; i>index; i--){
        [viewCtrs removeObjectAtIndex:i];
    }
    [viewCtrs addObject:toViewControllers];
    [self.navigationController setViewControllers:viewCtrs animated:YES];



没有更多推荐了,返回首页