首先先說說這個卡死的bug是如何觸發(fā)的;
Navigation 是一個由一系列Controller組成的棧,棧的原理是先進后出;舉個例子:一摞書放在地上,如果你想要拿出最后一本,就要先把上面的書先拿掉;
Navigation同樣,假如這個棧中有多個Controller,
側滑手勢interactivePopGestureRecognizer會將最上面的一個Controller移出棧并銷毀頁面,
問題就在這里,如果這個Navigation的棧中只有一個Controller,通過側滑手勢移除后,棧中就沒有了這個Controller,才導致了頁面的卡死;
知道了這個導致這個問題的原因后,再想要解決這個問題就很簡單,我們只需要找到側滑手勢interactivePopGestureRecognizer監(jiān)聽事件,保證Navigation棧中至少有一個Controller;
首先你的NavigationController要有個父類我這里命名為BaseNavigationViewController,在父類中加要用到的代理
@interface BaseNavigationViewController () <UINavigationControllerDelegate,UIGestureRecognizerDelegate>
@end
設置代理
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? __weak BaseNavigationViewController *weakSelf = self;
? ? if([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
? ? {
? ? ? ? self.interactivePopGestureRecognizer.delegate = weakSelf;
? ? ? ? self.delegate= weakSelf;
? ? }
}?
接著重寫NavigationControllerDelegate中展示頁面的方法
- (void)navigationController:(UINavigationController*)navigationController
?? ? ? didShowViewController:(UIViewController*)viewController
? ? ? ? ? ? ? ? ? ? animated:(BOOL)animate
{
? ? NSArray* ctrlArray = navigationController.viewControllers;
? ? if(ctrlArray.count>1) {
? ? ? ? if([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
? ? ? ? ? ? self.interactivePopGestureRecognizer.enabled = YES;
? ? ? ? }
? ? }else{
? ? ? ? if([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
? ? ? ? ? ? self.interactivePopGestureRecognizer.enabled = NO;
? ? ? ? }
? ? }
}
只要保證Navigation棧中至少有一個Controller即可
到這里問題就已經解決了