Can't add self as subview

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

crash日志

從日志上來(lái)看崩潰是在main函數(shù),定位不到具體的地方。

像這種crash,一般最簡(jiǎn)單地情況是:

[self.view addSubview:self.view];

這種確實(shí)會(huì)直接導(dǎo)致崩潰,但不是引起原因。

另一種錯(cuò)誤原因是說(shuō)一次push了兩次,動(dòng)畫(huà)被打斷后引起的crash。

頭文件
實(shí)現(xiàn)文件

對(duì)push的UIViewController來(lái)進(jìn)行進(jìn)行控制。


另一種方法:

創(chuàng)建一個(gè)分類,攔截控制器入棧\出棧的方法調(diào)用,通過(guò)安全的方式,確保當(dāng)有控制器正在進(jìn)行入棧\出棧操作時(shí),沒(méi)有其他入棧\出棧操作。

此分類用到運(yùn)行時(shí) (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


參考文件:

Can't add self as subview

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)至元數(shù)據(jù)結(jié)尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 1,776評(píng)論 0 9
  • 問(wèn)題描述:這個(gè)問(wèn)題非常常見(jiàn),就是平時(shí)我們做一個(gè)按鈕(我們假設(shè)這個(gè)頁(yè)面是RootVC),按鈕加一個(gè)事件,點(diǎn)擊這個(gè)事件...
    程序員學(xué)哥閱讀 2,439評(píng)論 0 0
  • 轉(zhuǎn)場(chǎng)過(guò)程解析 UINavigationController對(duì)于translation動(dòng)畫(huà)做了一定的封裝, 同時(shí)持有...
    Archerlly閱讀 6,742評(píng)論 24 10
  • 首先應(yīng)該檢查自己代碼是否出現(xiàn)誤操作 我這次的問(wèn)題是在tableViewController里 寫了一句 [self...
    3a169b0787bc閱讀 309評(píng)論 0 0
  • 今天在查看Umeng時(shí),錯(cuò)誤列表里出現(xiàn)了這個(gè)Can't add self as subview錯(cuò)誤(標(biāo)示沒(méi)有遇到過(guò)...
    LeoZzz閱讀 844評(píng)論 0 0