ios防止push多个相同重复界面

在ios中经常出现push多个相同页面,为了防止这种现象您可以如下设置

//自定义naviViewController,实现代理方法
#import "NaviViewController.h"

@interface NaviViewController () <UINavigationControllerDelegate>
// 记录push标志
@property (nonatomic, getter=isPushing) BOOL pushing;
@end

@implementation NaviViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.pushing == YES) {
        DDLogError(@"----------------------------------被拦截");
        return;
    } else {
        self.pushing = YES;
        [super pushViewController:viewController animated:animated];
    }
}
#pragma mark - UINavigationControllerDelegate
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    self.pushing = NO;
}
- (void)dealloc {
    self.navigationController.delegate = nil;
}

初始化tabBarController时

设置viewController的NavigationController为NaviViewController就可
MainViewController *mainVc = [[MainViewController alloc] init];
NaviViewController *nvc  = [[NaviViewController alloc] initWithRootViewController:mainVc];

欢迎互相学习Github

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。