美文网首页
防止navigation多次push一个页面

防止navigation多次push一个页面

作者: CodeGeass | 来源:发表于2018-01-10 10:40 被阅读73次

    场景:快速多次点击cell跳转到另一个页面,另一个页面被push多次。

    原因:push后的页面有耗时操作或者刚好push到另一个页面时,另一个页面正好在reloadData卡住主线程。造成点击cell时卡住了。

    解决方法:在基类导航控制器中重写导航控制器的push方法。

    #import "ZDBaseNavigationController.h"
    
    @interface ZDBaseNavigationController ()<UINavigationControllerDelegate>
    @property (nonatomic, getter=isPushing) BOOL pushing;///< 是否正在push,防止多次push同一个VC
    @end
    
    @implementation ZDBaseNavigationController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.delegate = self;
    }
    
    #pragma mark - Push
    - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
        if (self.isPushing) {
            NSLog(@"拦截多次push同一个VC");
            return;
        } else {
            self.pushing = YES;
        }
        
        [super pushViewController:viewController animated:animated];
    }
    
    - (void)navigationController:(UINavigationController *)navigationController
           didShowViewController:(UIViewController *)viewController
                        animated:(BOOL)animated
    {
        self.pushing = NO;
    }
    

    相关文章

      网友评论

          本文标题:防止navigation多次push一个页面

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