美文网首页
记一次连续pushViewController导致Can'

记一次连续pushViewController导致Can'

作者: 啊哈呵 | 来源:发表于2018-10-19 17:29 被阅读67次
    线上崩溃反馈
    96671211-F3FB-4532-BB8C-831DED659A0D.png
    猜想崩溃原因

    某一时刻、某一场景突然连续push多个界面动画相关导致(比如有说多个通知、多个回调一起执行连续push)

    问题重现简化代码,
    - (void)clickButton {
        
        ViewController *vc1 = ViewController.new;
        vc1.title = @"vc1";
    
        ViewController *vc2 = ViewController.new;
        vc2.title = @"vc2";
        
        ViewController *vc3 = ViewController.new;
        vc3.title = @"vc3";
    
        [self.navigationController pushViewController:vc1 animated:NO];
        [self.navigationController pushViewController:vc2 animated:YES];
        [self.navigationController pushViewController:vc3 animated:YES];
    
    }
    

    代码执行完在vc3点击返回vc2时候,发现vc2界面的view没有显示出来,继续点击vc2的返回按钮时候,就崩溃闪退了

    解决问题
    #import "ZYXNavigationController.h"
    
    @interface ZYXNavigationController ()<UINavigationControllerDelegate>
    @property (assign, nonatomic) BOOL isSwitching;
    @property (nonatomic, strong) NSMutableArray *pushVCArr;/**< */
    @end
    
    @implementation ZYXNavigationController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.pushVCArr = [NSMutableArray array];
        self.delegate = self;
    }
    // 重载 push 方法
    - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
        
        if (animated) {
            if (self.isSwitching) {
                [self.pushVCArr addObject:viewController];// 1. 如果是动画,并且正在切换,保存viewController
                return;
            }
            self.isSwitching = YES; // 2. 否则修改状态
        }
        [super pushViewController:viewController animated:animated];
    }
    
    #pragma mark - UINavigationControllerDelegate
    
    - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
        self.isSwitching = NO; // 3. 还原状态
        if (self.pushVCArr.count>0) {
            // 取出保存的vc,执行push
            UIViewController *vc = self.pushVCArr[0];
            [self pushViewController:viewController animated:YES];
            [self.pushVCArr removeObject:vc];
        }
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:记一次连续pushViewController导致Can'

          本文链接:https://www.haomeiwen.com/subject/uykuzftx.html