Can't add self as subview

最近在iOS的項目中出現了Can't add self as subview 的crash,日志信息如下

crash日志

從日志上來看崩潰是在main函數,定位不到具體的地方。

像這種crash,一般最簡單地情況是:

[self.view addSubview:self.view];

這種確實會直接導致崩潰,但不是引起原因。

另一種錯誤原因是說一次push了兩次,動畫被打斷后引起的crash。

頭文件
實現文件

對push的UIViewController來進行進行控制。


另一種方法:

創建一個分類,攔截控制器入棧\出棧的方法調用,通過安全的方式,確保當有控制器正在進行入棧\出棧操作時,沒有其他入棧\出棧操作。

此分類用到運行時 (Runtime) 的方法交換Method Swizzling,因此只需要復制下面的代碼到自己的項目中,此 bug 就不復存在了。

#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


參考文件:

Can't add self as subview

Can't Add Self as Subview 崩潰解決辦法

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容