Can't add self as subview

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

crash日志

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

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

[self.view addSubview:self.view];

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

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

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

對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


參考文件:

Can't add self as subview

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末嘉汰,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子状勤,更是在濱河造成了極大的恐慌鞋怀,老刑警劉巖双泪,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異焙矛,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)残腌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進(jìn)店門抛猫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人逾滥,你說我怎么就攤上這事败匹。” “怎么了毅待?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵尸红,是天一觀的道長刹泄。 經(jīng)常有香客問我,道長特石,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任墩莫,我火速辦了婚禮狂秦,結(jié)果婚禮上推捐,老公的妹妹穿的比我還像新娘。我一直安慰自己堪簿,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布哪审。 她就那樣靜靜地躺著虑瀑,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上描馅,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天铭污,我揣著相機(jī)與錄音,去河邊找鬼嘹狞。 笑死,一個胖子當(dāng)著我的面吹牛谈截,可吹牛的內(nèi)容都是我干的涧偷。 我是一名探鬼主播,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼喻鳄,長吁一口氣:“原來是場噩夢啊……” “哼除呵!你這毒婦竟也來了爪喘?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤泛啸,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后吕粹,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體岗仑,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年稳其,在試婚紗的時候發(fā)現(xiàn)自己被綠了既鞠。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片盖文。...
    茶點(diǎn)故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖洒敏,靈堂內(nèi)的尸體忽然破棺而出疙驾,到底是詐尸還是另有隱情,我是刑警寧澤函荣,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布扳肛,位于F島的核電站,受9級特大地震影響踊谋,放射性物質(zhì)發(fā)生泄漏旋讹。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一睦疫、第九天 我趴在偏房一處隱蔽的房頂上張望鞭呕。 院中可真熱鬧,春花似錦、人聲如沸底洗。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至圣贸,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間昼激,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留耕餐,地道東北人辟狈。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像明未,于是被迫代替她去往敵國和親壹蔓。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,512評論 2 359

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