崩溃无法在ios中将自己添加为子视图



无法再现以下崩溃。

我已经处理过这种情况:当一个视图控制器正在制作动画时,不分割视图控制器这里提到的类似问题:iOS应用程序错误-Can';t将self添加为子视图。我已经实现了这个安全分段的解决方案。

尽管如此,我还是被追尾了。

注意:iOS 7和8都会崩溃,但iOS 8会发生更多崩溃。(如果有帮助的话)。即使从viewDidLoad中分离,也不会在模拟器中崩溃。

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't add self as subview'
Application Specific Backtrace 1:
0   CoreFoundation                      0x24503f87 <redacted> + 126
1   libobjc.A.dylib                     0x31c62c77 _objc_exception_throw + 38
2   CoreFoundation                      0x24503ecd -[NSException initWithCoder:] + 0
3   UIKit                               0x279880b3 -[UIView _addSubview:positioned:relativeTo:] + 114
4   UIKit                               0x27988037 -[UIView addSubview:] + 30
5   UIKit                               0x27b4d491 <redacted> + 1236
6   UIKit                               0x2798e701 +[UIView performWithoutAnimation:] + 72
7   UIKit                               0x27b4cd79 -[_UINavigationParallaxTransition animateTransition:] + 808
8   UIKit                               0x27b0b787 -[UINavigationController _startCustomTransition:] + 2854
9   UIKit                               0x27a2ab2f -[UINavigationController _startDeferredTransitionIfNeeded:] + 422
10  UIKit                               0x27a2a931 -[UINavigationController __viewWillLayoutSubviews] + 44
11  UIKit                               0x27a2a8c9 -[UILayoutContainerView layoutSubviews] + 184
12  UIKit                               0x2797f25f -[UIView layoutSublayersOfLayer:] + 514
13  QuartzCore                          0x273aa1d5 -[CALayer layoutSublayers] + 136
14  QuartzCore                          0x273a5bd1 <redacted> + 360
15  QuartzCore                          0x273a5a59 <redacted> + 16
16  QuartzCore                          0x273a5447 <redacted> + 222
17  QuartzCore                          0x273a5251 <redacted> + 324
18  UIKit                               0x27980c31 <redacted> + 1384
19  CoreFoundation                      0x244ca807 <redacted> + 14
20  CoreFoundation                      0x244c9c1b <redacted> + 222
21  CoreFoundation                      0x244c8299 <redacted> + 768
22  CoreFoundation                      0x24415db1 _CFRunLoopRunSpecific + 476
23  CoreFoundation                      0x24415bc3 _CFRunLoopRunInMode + 106
24  GraphicsServices                    0x2b7a0051 _GSEventRunModal + 136
25  UIKit                               0x279e0f01 _UIApplicationMain + 1440

当出于某种原因多次按下segue/viewcontroller时,也会发生这种情况。其中一种情况是,当你观察通知时,如果由于某种原因,该通知被发布了多次,并且在观察方法中,如果你推送视图控制器,它会推送不止一次,最终会导致这种崩溃。

很抱歉聚会迟到了。我最近遇到了这个问题,因为同时推送多个视图控制器,我的导航栏进入损坏状态。发生这种情况的原因是,当第一个视图控制器仍在设置动画时,会推动另一个视图控制器。从这里的非善意回答中得到提示

我想出了一个简单的解决方案,适用于我的情况。您只需要子类化UINavigationController并覆盖pushViewController方法,然后检查以前的视图控制器动画是否已经完成。您可以通过使类成为UINavigationControllerDelegate的委托并将该委托设置为self来收听动画完成。

我在这里上传了一个要点,使事情变得简单。

只要确保将这个新类设置为故事板中的NavigationController即可。

最新更新