最近在iOS的項(xiàng)目中出現(xiàn)了Can't add self as subview 的crash,日志信息如下
從日志上來看崩潰是在main函數(shù)夹界,定位不到具體的地方也拜。
像這種crash蔓钟,一般最簡單地情況是:
[self.view addSubview:self.view];
這種確實(shí)會直接導(dǎo)致崩潰兰绣,但不是引起原因。
另一種錯誤原因是說一次push了兩次,動畫被打斷后引起的crash叠穆。
對push的UIViewController來進(jìn)行進(jìn)行控制硼被。
另一種方法:
創(chuàng)建一個分類,攔截控制器入棧\出棧的方法調(diào)用讶请,通過安全的方式祷嘶,確保當(dāng)有控制器正在進(jìn)行入棧\出棧操作時,沒有其他入棧\出棧操作夺溢。
此分類用到運(yùn)行時 (Runtime) 的方法交換Method Swizzling论巍,因此只需要復(fù)制下面的代碼到自己的項(xiàng)目中,此 bug 就不復(fù)存在了风响。
#import ?"UINavigationController+Consistent.h"
#import ?<objc/runtime.h>
/// This char is used to add storage for the is PushingViewController property.
static char const *const ObjectTagKey ="ObjectTag";
@interfaceUINavigationController ()
@property(readwrite, getter= isViewTransitionInProgress) BOOL viewTransitionInProgress;
@end
@implementation ?UINavigationController (Consistent)
- (void)setViewTransitionInProgress:(BOOL)property {
? ? ? ? ? ? NSNumber *number = [NSNumber numberWithBool:property];
? ? ? ? ? ?objc_setAssociatedObject(self, ObjectTagKey, number , OBJC_ASSOCIATION_RETAIN);
}
- (BOOL)isViewTransitionInProgress {
? ? ? ? ? NSNumber *number = objc_getAssociatedObject(self, ObjectTagKey);
? ? ? ? return ? [number boolValue];
}
#pragma mark - Intercept Pop, Push, PopToRootVC
/// @name Intercept Pop, Push, PopToRootVC
- (NSArray *)safePopToRootViewControllerAnimated:(BOOL)animated {
? ? ?if(self.viewTransitionInProgress) ? return ? ?nil;
? ? ?if(animated) {
? ? ? ? ? ? ? self.viewTransitionInProgress =YES;
? ? ? ?}
//-- This is not a recursion, due to method swizzling the call below calls the originalmethod.
? ? ? return ?[self ?safePopToRootViewControllerAnimated:animated];
}
- (NSArray *)safePopToViewController:(UIViewController *)viewController animated:(BOOL)animated {
? ? ? ? ?if(self.viewTransitionInProgress) ?return ?nil; ? ??
? ? ? ?if(animated) {
? ? ? ? ? ? ? ? ? self.viewTransitionInProgress = YES;
? ? ? }
//-- This is not a recursion, due to method swizzling the call below calls the originalmethod.
? ? ? return [self ? safePopToViewController:viewController animated:animated];
}
- (UIViewController *)safePopViewControllerAnimated:(BOOL)animated {
? ? ? if(self.viewTransitionInProgress) ? ? return ? ?nil;?
? ? ?if(animated) {
? ? ? ? ? ? ? self.viewTransitionInProgress =YES;
? ? ?}
//-- This is not a recursion, due to method swizzling the call below calls the originalmethod.
? ? ? return ?[self ?safePopViewControllerAnimated:animated];
}
- (void)safePushViewController:(UIViewController *)viewController animated:(BOOL)animated {
? ? ? ? ?self.delegate =self;
//-- If we are already pushing a view controller, we dont push another one.
? ? ? ? if(self.isViewTransitionInProgress ==NO) {
//-- This is not a recursion, due to method swizzling the call below calls the originalmethod.
? ? ?[self ? safePushViewController:viewController animated:animated];
? ? ?if(animated) {
? ? ?self.viewTransitionInProgress =YES;
? ? ?}
? ?}
}
// This is confirmed to be App Store safe.
// If you feel uncomfortable to use Private API, you could also use the delegate method navigationController:didShowViewController:animated:.
- (void)safeDidShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//-- This is not a recursion. Due to method swizzling this is calling the original method.
? ? ?[self ?safeDidShowViewController:viewController animated:animated];?
? ? self.viewTransitionInProgress =NO;
}
// If the user doesnt complete the swipe-to-go-back gesture, we need to intercept it and set the flag to NO again.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
? ?id tc = navigationController.topViewController.transitionCoordinator;
? ?[tc notifyWhenInteractionEndsUsingBlock:^(id context) {
? ? ? ? ? ? ? ?self.viewTransitionInProgress =NO;
? ? ? ? ? ? ? //--Reenable swipe back gesture.
? ? ? ? ? ? ?self.interactivePopGestureRecognizer.delegate = (id)viewController;
? ? ? ? ? ? [self.interactivePopGestureRecognizer setEnabled:YES];
}];
//-- Method swizzling wont work in the case of a delegate so:?
? //-- forward this method to the original delegate if there is one different than ourselves.
? ? ? if(navigationController.delegate !=self) {
? ? ? [navigationController.delegate navigationController:navigationController
? ? ? ? ? ? ? ?willShowViewController:viewController
? ? ? ? ? ? ? ? animated:animated];
? ? ? ? }
}
+ (void)load {
//-- Exchange the original implementation with our custom one.
method_exchangeImplementations(class_getInstanceMethod(self,@selector(pushViewController:animated:)),class_getInstanceMethod(self,@selector(safePushViewController:animated:)));
method_exchangeImplementations(class_getInstanceMethod(self,@selector(didShowViewController:animated:)),class_getInstanceMethod(self,@selector(safeDidShowViewController:animated:)));
method_exchangeImplementations(class_getInstanceMethod(self,@selector(popViewControllerAnimated:)),class_getInstanceMethod(self,@selector(safePopViewControllerAnimated:)));
method_exchangeImplementations(class_getInstanceMethod(self,@selector(popToRootViewControllerAnimated:)),class_getInstanceMethod(self,@selector(safePopToRootViewControllerAnimated:)));
method_exchangeImplementations(class_getInstanceMethod(self,@selector(popToViewController:animated:)),class_getInstanceMethod(self,@selector(safePopToViewController:animated:)));
}
@end
參考文件: