Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting crash can't add self as subview in ios

Can't reproduce following crash.

I already handled the case : not segueing the viewcontroller while one viewcontroller is animating. Similar problem mentioned here: iOS app error - Can't add self as subview. I have implemented this solution for safe segueing.

Still I am getting following crash.

Note: getting crash on both iOS 7 and 8 but more crash occurrence for iOS 8.(if that helps). Not getting crash in simulator even if segueing from 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
like image 645
Pushparaj Avatar asked Oct 01 '14 10:10

Pushparaj


2 Answers

This also happens when for some reason you push the segue / viewcontroller multiple times. One of such case is when you are observing for a notification and if for some reason that notification was posted multiple times and in the observing method, if you were pushing a view controller, it will push to more than once which eventually will cause this kind of crash.

like image 162
Anand Avatar answered Nov 05 '22 04:11

Anand


Sorry for being late for the party. I recently had this issue wherein my navigationbar goes into corrupted state because of pushing more than one view controller at the same time. This happens because the other view controller is pushed while the first view controller is still animating. Taking hint from the nonamelive answer from here

I came up with my simple solution that works in my case. You just need to subclass UINavigationController and override the pushViewController method and check if previous view controller animation is finished as yet. You can listen to the animation completion by making your class a delegate of UINavigationControllerDelegate and setting the delegate to self.

I have uploaded a gist here to make things simple.

Just make sure you set this new class as the NavigationController in your storyboard.

like image 1
nikhil.thakkar Avatar answered Nov 05 '22 04:11

nikhil.thakkar