UINavigationController setViewControllers:animated:

最近发现一个 bug,setViewControllers:animated: 不生效

复现场景:

需要删除 上一个 controller,于是在 viewDidAppear 中

NSMutableArray *array = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
        if (_removePreviousControllerCount < array.count-1) {
            self.hidesBottomBarWhenPushed = YES;
            [array removeObjectsInRange:NSMakeRange(array.count -1 - _removePreviousControllerCount, _removePreviousControllerCount)];
            [self.navigationController setViewControllers:array animated:NO];
        }

但是如果这时候弹出一个 UIAlertController 的话,那么 这个设置就不生效了。这个网上也没有人发现是什么问题。于是自己想了一个解决方案,在 alertController 消失的时候,再来删除一次。

但是在 iOS8 系统上面没有这个问题。所以再次删除的时候,还需要判断前面是否真的已经删除成功

- (void)viewDidLoad
{
...
self.origionViewControllers = [self viewControllers];
...
}

#pragma mark - viewcontrollers
- (BOOL)isRemoveSuccess
{
    if (_removePreviousControllerCount > 0) {
        NSArray *current = [self viewControllers];
        if ([self isViewControllers:current equalTo:self.origionViewControllers]) {
            // 一样,说明没有删除成功
            return NO;
        }
    }
    return YES;
}

- (BOOL)isViewControllers:(NSArray *)arr1 equalTo:(NSArray *)arr2
{
    if (arr1.count != arr2.count) {
        return NO;
    }
    for (int i = 0; i *)viewControllers
{
    NSMutableArray *mutArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < self.navigationController.viewControllers.count; i++) {
        UIViewController *vc = self.navigationController.viewControllers[i];
        NSString *address = [NSString stringWithFormat:@"%@",vc];
        [mutArr addObject:address];
        if (vc == self) {
            // 后面的就先不管了。
            break;
        }
    }
    return mutArr;
}
- (void)removeViewControllers
{
    if (_removePreviousControllerCount > 0) {
        NSMutableArray *array = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
        if (_removePreviousControllerCount < array.count-1) {
            self.hidesBottomBarWhenPushed = YES;
            [array removeObjectsInRange:NSMakeRange(array.count -1 - _removePreviousControllerCount, _removePreviousControllerCount)];
            [self.navigationController setViewControllers:array animated:NO];
        }
    }
}

你可能感兴趣的:(UINavigationController setViewControllers:animated:)