关于模态切换视图遇到的问题

最近遇到一个自定义类Tabbar的视图,于是用模态方式去自由切换viewController,关键方法是:

- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(5_0);

不过在完成之后 来回快速点击会发现崩溃的问题
Unbalanced calls to begin/end appearance transitions for xxx
发现 transitionFromViewController 这个方法若当前viewController已在最前,就不应该在调用该方法,于是解决办法如下:

- (void)replaceFromViewController:(ALYBaseViewController *)first toViewController:(ALYBaseViewController *)second index:(NSInteger)idx completion:(void (^)(ALYBaseViewController *, ALYBaseViewController *, NSInteger))completion{
    // 判断是否是一个vc
    if ([second isEqual:first] return;
    
    [self addChildViewController:second];
    [self transitionFromViewController:first toViewController:second duration:0 options:UIViewAnimationOptionTransitionNone animations:^{
        [second didMoveToParentViewController:self];
        [first willMoveToParentViewController:nil];
        [first removeFromParentViewController];

        completion(first, second, idx);
    } completion:^(BOOL finished) {
        if (finished) {
        }
    }];
}

记录一下。

你可能感兴趣的:(关于模态切换视图遇到的问题)