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

2019-03-06  本文已影响0人  有梦才可以远航

在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

上一篇 下一篇

猜你喜欢

热点阅读