ios pushViewController 跳转导致重复打开多个页面

在ios开发中使用pushViewController方法打开UIViewController界面的时候,我们由于点击跳转按钮速度比较快,可能会出现UIViewController界面重复打开问题,那么我们该如何解决ios UIViewController界面重复跳转问题呢?

首先我们要了解的是pushViewController方法是UINavigationController类里面的方法,要想防止界面的重复跳转,需要新建一个UINavigationController类的category分类,如图所示:

 

 

 一、中定义一个UINavigationController类的扩展方法:

       .h文件

@interface UINavigationController (NoRepeat)
//是否允许重复的方法定义
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated noRepeatOpen:(BOOL)flag;
@end

   .m文件

#import "UINavigationController+NoRepeat.h"

@implementation UINavigationController (NoRepeat)
//“orderQueryVC”是目标controller界面,"YES"表示不允许重复打开
//[self.navigationController pushViewController:orderQueryVC animated:YES noRepeatOpen:YES];

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated noRepeatOpen:(BOOL)flag{
    //判断该类是否已经打开,
    if ([[self.viewControllers lastObject] isKindOfClass:viewController.class] && flag) {
        return;
    }
    //隐藏下方tab,可忽略
    if (self.viewControllers.count) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    //跳转
    [self pushViewController:viewController animated:animated];
}

@end

二、引入

#import "UINavigationController+NoRepeat.h"

 

三、在BaseViewConstroller中封装

#pragma mark --------- 跳转页面
- (void)pushViewController:(UIViewController *)viewController noRepeatOpen:(BOOL)flag {
    //“orderQueryVC”是目标controller界面,"YES"表示不允许重复打开
    [self.navigationController pushViewController:viewController animated:NO noRepeatOpen:flag];
}

 

四、使用

 TemporaryPayDetailsViewController *temporaryPayDetailsViewController=[[TemporaryPayDetailsViewController alloc]init];
    [self pushViewController:temporaryPayDetailsViewController noRepeatOpen:YES];

 

posted @ 2021-09-08 09:34  张亚楠  阅读(570)  评论(0编辑  收藏  举报