Can't add self as subview的问题

原因一:加入了本身的视图

[self.view addSubview:self.view];

原因二:重复推出了控制器
下面的方法能复现bug

[self.navigationController pushViewController:set animated:YES];
    //0.3秒后重复推出(正常是0.5)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.navigationController pushViewController:set animated:YES];
    });

解决:
1.Commond + N 新建Objective-C File文件


Can't add self as subview的问题_第1张图片
张小牛.png

2.UINavigationController+ZXN中添加如下代码

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated isFixed:(id)isFixed{
    if (!isFixed || self.topViewController == isFixed){
        [self pushViewController:viewController animated:animated];
    }
}

- (id)navigationlock{
    return self.topViewController;
}

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated isFixed:(id)isFixed{
    if (!isFixed || self.topViewController == isFixed){
        [self popToViewController:viewController animated:animated];
    }
    return nil;
}

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated navigationLock:(id)isFixed{
    if (!isFixed || self.topViewController == isFixed) {
        [self popToRootViewControllerAnimated:animated];
    }
    return nil;
}

3.用法

id isFixed = [self.navigationController isFixed];
[self.navigationController pushViewController:compet animated:YES isFixed: isFixed];

4.检验

 id isFixed = [self.navigationController isFixed];
[self.navigationController pushViewController:compet animated:YES isFixed: isFixed];
    //0.3秒后重复推出(正常是0.5)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
       [self.navigationController pushViewController:compet animated:YES isFixed: isFixed];
 });

你可能感兴趣的:(Can't add self as subview的问题)