重写导航控制器的push方法,通过获取导航的代理方法来防止多次push界面的bug
.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface CoustomNavVC : UINavigationController
@end
NS_ASSUME_NONNULL_END
实现文件
#import "CoustomNavVC.h"
@interface CoustomNavVC () <UINavigationControllerDelegate>
@end
@implementation CoustomNavVC
- (void)viewDidLoad
{
[super viewDidLoad];
self.delegate = self;
}
//支持旋转
-(BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
//2019/9/5
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
//防止多次push同一个控制器
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
//这个方法是在当前控制器执行push的时候,禁止手势右划返回,避免出现crash的现象
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.enabled = NO;
}
//判断上一个控制器和现在的控制器是不是同一个,如果是,返回。如果不是push到当前控制器,这就有效避免了同一个控制器连续push的问题
if ([self.topViewController isMemberOfClass:[viewController class]]) {
return;
}
[super pushViewController:viewController animated:animated];
}
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.enabled = YES;
}
}