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

最近遇到一个自定义类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) {
        }
    }];
}

记录一下。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容