prepareForSegue called before viewDidLoad - ios

I've tried seating SO and Google but it seems no one has had this issue.
I have two ViewControllers in my iOS app.
In viewControllerA on a tap event I call:
[self performSegueWithIdentifier:#"fooSegue" sender:self];
Which this calls:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
on viewControllerA
No problem there.
However after that method,
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
is called on viewControllerB before viewDidLoad is even called. I don't need this mothod to run on this viewController at this stage. Along with that, viewDidLoad / viewWillApear are called twice after the prepareForSegue has been called on viewControllerB
I tried doing a stack strace to see who is calling it - but this is what I got:
* thread #1: tid = 0x5eac3, 0x0001e859 TestApp`-[viewControllerB prepareForSegue:sender:](self=0x0dad98b0, _cmd=0x0188185a, segue=0x0ce9e6f0, sender=0x0dad98b0) + 89 at viewControllerB.m:444, queue = 'com.apple.main-thread', stop reason = breakpoint 3.1 * frame #0: 0x0001e859 TestApp`-[ViewControllerB prepareForSegue:sender:](self=0x0dad98b0, _cmd=0x0188185a, segue=0x0ce9e6f0, sender=0x0dad98b0) + 89 at ViewControllerB.m:444
frame #1: 0x015f8efa UIKit`-[UIStoryboardSegueTemplate _perform:] + 156
frame #2: 0x015f8f87 UIKit`-[UIStoryboardSegueTemplate perform:] + 115
frame #3: 0x011b32e2 UIKit`-[UIViewController loadViewIfRequired] + 605
frame #4: 0x011b35d9 UIKit`-[UIViewController view] + 35
frame #5: 0x011cd942 UIKit`-[UINavigationController _startCustomTransition:] + 778
frame #6: 0x011da8f7 UIKit`-[UINavigationController _startDeferredTransitionIfNeeded:] + 688
frame #7: 0x011db4e9 UIKit`-[UINavigationController __viewWillLayoutSubviews] + 57
frame #8: 0x0131c0d1 UIKit`-[UILayoutContainerView layoutSubviews] + 213
frame #9: 0x01103964 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
frame #10: 0x023e682b libobjc.A.dylib`-[NSObject performSelector:withObject:] + 70
frame #11: 0x0286245a QuartzCore`-[CALayer layoutSublayers] + 148
frame #12: 0x02856244 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 380
frame #13: 0x028560b0 QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 26
frame #14: 0x027bc7fa QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 294
frame #15: 0x027bdb85 QuartzCore`CA::Transaction::commit() + 393
frame #16: 0x0287b5b0 QuartzCore`+[CATransaction flush] + 52
frame #17: 0x010929bb UIKit`_UIApplicationHandleEventQueue + 13095
frame #18: 0x02a1e77f CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
+ 15
frame #19: 0x02a1e10b CoreFoundation`__CFRunLoopDoSources0 + 235
frame #20: 0x02a3b1ae CoreFoundation`__CFRunLoopRun + 910
frame #21: 0x02a3a9d3 CoreFoundation`CFRunLoopRunSpecific + 467
frame #22: 0x02a3a7eb CoreFoundation`CFRunLoopRunInMode + 123
frame #23: 0x04bca5ee GraphicsServices`GSEventRunModal + 192
frame #24: 0x04bca42b GraphicsServices`GSEventRun + 104
frame #25: 0x01094f9b UIKit`UIApplicationMain + 1225
frame #26: 0x0011112d TestApp`main(argc=1, argv=0xbfffedf8) + 141 at main.m:16
I'm not sure what to make of this.
Can anyone asset with whats going on here?

If the destination scene has a "container view" that you added via Interface Builder, that includes an embed segue, which is automatically called when you transition to the parent view controller. This segue will trigger the instantiation of the child view controller and its view(s) and this process ends up calling prepareForSegue on the parent view controller, giving it a chance to pass whatever information the child view controller might need (if anything).
You should see viewDidLoad, viewDidAppear, etc., to be called on the destination's parent view controller, as well as on the child view controller.

Related

UINavigationController method setToolbarHidden bug in Xcode 9: infinite calculation of auto-layout constraints leads to OOM

I have an instance of UINavigationController nested in UITabBarController. I use the navigation controller to reach some view controller (tab bar still visible), from which I segue to a second view controller (tab bar no longer visible).
In the second view controller, as soon as I make a call to:
[self.navigationController setToolbarHidden:NO]
the app freezes and memory grows until OOM exception crashes it.
I acknowledge that nesting the navigation controller inside the tab bar is not recommended, but this setup seemed to work fine until iOS 11.
EDIT: when stopping execution, I see a lot of calls to:
UIView(UIConstraintBasedLayout)
UIView(AdditionalLayerSupport)
NSLayoutConstraint
Here's the full stack trace
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP * frame #0: 0x0000000106dd895c libobjc.A.dylib`objc_msgSend
+ 28
frame #1: 0x00000001067b6b9b Foundation`-[NSConcreteMapTable removeObjectForKey:] + 138
frame #2: 0x00000001069e6019 Foundation`_substituteOutAllOccurencesOfBodyVar + 1282
frame #3: 0x00000001067f3c5b Foundation`-[NSISEngine tryAddingDirectly:] + 144
frame #4: 0x00000001067f332f Foundation`-[NSISEngine tryToAddConstraintWithMarker:expression:integralizationAdjustment:mutuallyExclusiveConstraints:]
+ 440
frame #5: 0x00000001069f2067 Foundation`-[NSLayoutConstraint _addLoweredExpression:toEngine:integralizationAdjustment:lastLoweredConstantWasRounded:mutuallyExclusiveConstraints:]
+ 273
frame #6: 0x00000001067ea601 Foundation`-[NSLayoutConstraint _addToEngine:integralizationAdjustment:mutuallyExclusiveConstraints:] + 240
frame #7: 0x0000000109c9488d UIKit`__57-[UIView(AdditionalLayoutSupport)
_switchToLayoutEngine:]_block_invoke_2 + 452
frame #8: 0x00000001067f0de1 Foundation`-[NSISEngine withBehaviors:performModifications:] + 131
frame #9: 0x0000000109c946a2 UIKit`__57-[UIView(AdditionalLayoutSupport)
_switchToLayoutEngine:]_block_invoke + 604
frame #10: 0x0000000109c9441e UIKit`-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 223
frame #11: 0x00000001091ed84f UIKit`__45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 112
frame #12: 0x00000001067f0de1 Foundation`-[NSISEngine withBehaviors:performModifications:] + 131
frame #13: 0x00000001091ed778 UIKit`-[UIView(Hierarchy) _postMovedFromSuperview:] + 855
frame #14: 0x00000001091fe031 UIKit`-[UIView(Internal) _addSubview:positioned:relativeTo:] + 1927
frame #15: 0x0000000109b507e1 UIKit`-[_UILayoutArrangement insertItem:atIndex:] + 502
frame #16: 0x0000000109ca1b4d UIKit`__50-[_UIOrderedLayoutArrangement insertItem:atIndex:]_block_invoke + 50
frame #17: 0x0000000109ca18df UIKit`-[_UIOrderedLayoutArrangement _trackChangesAffectingExternalBaselineConstraints:] + 320
frame #18: 0x0000000109ca1aea UIKit`-[_UIOrderedLayoutArrangement insertItem:atIndex:] + 478
frame #19: 0x000000010982edea UIKit`-[UIStackView insertArrangedSubview:atIndex:] + 283
frame #20: 0x0000000109b29972 UIKit`-[_UIButtonBar _layoutBar] + 3639
frame #21: 0x0000000109b2bb44 UIKit`-[_UIButtonBarStackView updateConstraints] + 48
frame #22: 0x0000000109c958b6 UIKit`-[UIView(AdditionalLayoutSupport)
_sendUpdateConstraintsIfNecessaryForSecondPass:] + 161
frame #23: 0x0000000109c95ed2 UIKit`-[UIView(AdditionalLayoutSupport)
_updateConstraintsIfNeededCollectingViews:forSecondPass:] + 1296
frame #24: 0x0000000109c95d51 UIKit`-[UIView(AdditionalLayoutSupport)
_updateConstraintsIfNeededCollectingViews:forSecondPass:] + 911
frame #25: 0x0000000109c95d51 UIKit`-[UIView(AdditionalLayoutSupport)
_updateConstraintsIfNeededCollectingViews:forSecondPass:] + 911
frame #26: 0x0000000109c95d51 UIKit`-[UIView(AdditionalLayoutSupport)
_updateConstraintsIfNeededCollectingViews:forSecondPass:] + 911
frame #27: 0x00000001067f0de1 Foundation`-[NSISEngine withBehaviors:performModifications:] + 131
frame #28: 0x0000000109c96703 UIKit`__100-[UIView(AdditionalLayoutSupport)
_updateConstraintsIfNeededWithViewForVariableChangeNotifications:]_block_invoke
+ 90
frame #29: 0x0000000109c94f61 UIKit`-[UIView(AdditionalLayoutSupport)
_withUnsatisfiableConstraintsLoggingSuspendedIfEngineDelegateExists:] + 104
frame #30: 0x0000000109c96272 UIKit`-[UIView(AdditionalLayoutSupport)
_updateConstraintsIfNeededWithViewForVariableChangeNotifications:] + 160
frame #31: 0x0000000109c9738c UIKit`-[UIView(AdditionalLayoutSupport)
_updateConstraintsAtEngineLevelIfNeededWithViewForVariableChangeNotifications:]
+ 401
frame #32: 0x00000001091ef1b6 UIKit`-[UIView(Hierarchy) layoutBelowIfNeeded] + 1517
frame #33: 0x000000010957b35e UIKit`-[_UIButtonBarButton willMoveToWindow:] + 63
frame #34: 0x00000001091ec996 UIKit`-[UIView(Hierarchy) _willMoveToWindow:] + 861
frame #35: 0x00000001091eb493 UIKit`__UIViewWillBeRemovedFromSuperview + 484
frame #36: 0x00000001091eb0ea UIKit`-[UIView(Hierarchy) removeFromSuperview] + 95
frame #37: 0x0000000109b295d3 UIKit`-[_UIButtonBar _layoutBar] + 2712
frame #38: 0x0000000109b2bb44 UIKit`-[_UIButtonBarStackView updateConstraints] + 48
frame #39: 0x0000000109c958b6 UIKit`-[UIView(AdditionalLayoutSupport)
_sendUpdateConstraintsIfNecessaryForSecondPass:] + 161
frame #40: 0x0000000109c95ed2 UIKit`-[UIView(AdditionalLayoutSupport)
_updateConstraintsIfNeededCollectingViews:forSecondPass:] + 1296
frame #41: 0x0000000109c95d51 UIKit`-[UIView(AdditionalLayoutSupport)
_updateConstraintsIfNeededCollectingViews:forSecondPass:] + 911
frame #42: 0x0000000109c95d51 UIKit`-[UIView(AdditionalLayoutSupport)
_updateConstraintsIfNeededCollectingViews:forSecondPass:] + 911
frame #43: 0x0000000109c95d51 UIKit`-[UIView(AdditionalLayoutSupport)
_updateConstraintsIfNeededCollectingViews:forSecondPass:] + 911
frame #44: 0x00000001067f0de1 Foundation`-[NSISEngine withBehaviors:performModifications:] + 131
frame #45: 0x0000000109c96703 UIKit`__100-[UIView(AdditionalLayoutSupport)
_updateConstraintsIfNeededWithViewForVariableChangeNotifications:]_block_invoke
+ 90
frame #46: 0x0000000109c94f61 UIKit`-[UIView(AdditionalLayoutSupport)
_withUnsatisfiableConstraintsLoggingSuspendedIfEngineDelegateExists:] + 104
frame #47: 0x0000000109c96272 UIKit`-[UIView(AdditionalLayoutSupport)
_updateConstraintsIfNeededWithViewForVariableChangeNotifications:] + 160
frame #48: 0x0000000109c9738c UIKit`-[UIView(AdditionalLayoutSupport)
_updateConstraintsAtEngineLevelIfNeededWithViewForVariableChangeNotifications:]
+ 401
frame #49: 0x00000001091efa5b UIKit`-[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 159
frame #50: 0x00000001095742d5 UIKit`-[UILayoutContainerView layoutSubviews] + 270
frame #51: 0x0000000109204551 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1331
frame #52: 0x00000001064db4ba QuartzCore`-[CALayer layoutSublayers] + 153
frame #53: 0x00000001064df5a9 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 401
frame #54: 0x00000001064681cd QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 365
frame #55: 0x0000000106493ae4 QuartzCore`CA::Transaction::commit() + 500
frame #56: 0x0000000109160687 UIKit`_afterCACommitHandler + 272
frame #57: 0x00000001080f8db7 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__
+ 23
frame #58: 0x00000001080f8d0e CoreFoundation`__CFRunLoopDoObservers + 430
frame #59: 0x00000001080dd324 CoreFoundation`__CFRunLoopRun + 1572
frame #60: 0x00000001080dca89 CoreFoundation`CFRunLoopRunSpecific + 409
frame #61: 0x000000010dc429c6 GraphicsServices`GSEventRunModal + 62
frame #62: 0x0000000109135d30 UIKit`UIApplicationMain + 159
frame #63: 0x0000000101ff6bf9 MyAppName`main(argc=1, argv=0x00007fff5de3e0a8) at main.m:23
frame #64: 0x000000010f453d81 libdyld.dylib`start + 1
frame #65: 0x000000010f453d81 libdyld.dylib`start + 1
This is the answer from a developer from the same team as OP is from.
After one more round of research we have found that the problem was on our side:
- (NSArray *)toolbarItems
{
return [self toolbarItemsWithRunningAdditionalAnimation:NO];
}
Original developer made the method toolbarItemsWithRunningAdditionalAnimation to return new objects each time the method was called. When going to this controller iOS calls toolbarItems at least 3 times so giving it new objects every time we made iOS confused so it was recalculating auto-layout constraints in an infinite loop. Our original fix below is also working, however it becomes obsolete as we start always returning the same array of items like:
- (NSArray *)toolbarItems {
if (_cachedToolbarItems) { return _cachedToolbarItems; }
_cachedToolbarItems = [self toolbarItemsWithRunningAdditionalAnimation:NO];
return _cachedToolbarItems'
}
We are doing the following in our app (pseudocode):
UIToolbar *toolbar = self.navigationController.toolbar;
NSArray <UIBarButtonItem *> *items = #[
flexibleSpace,
share,
flexibleSpace,
play,
flexibleSpace,
stats,
flexibleSpace
];
[toolbar setItems:items animated:animated];
[self.navigationController setToolbarHidden:NO animated:animated];
The problematic items that caused infinite calculation of auto-layout constraints were share and stats. The thing they both had in common - they both were created using -[UIBarButtonItem initWithImage:style:target:action:] initializer. When we started using another initializer:
UIButton *customShareButton = ... // we create button ourselves.
UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithCustomView: customShareButton];
The problem went away.
Besides the accepted answer, we found that our problem was related to the fact that in our class, we override the method
- (NSArray *)toolbarItems.
Every time our method is called, a new set of toolbarItems is created.
Seems like from iOS 11, every time new toolbar items are returned to this method, UINavigationController:layoutIfNeeded is called again, which in turn calls toolbarItems which returns new items because of our implementation. This causes an infinite loop.
If you encounter this problem, check if you are overriding - (NSArray *)toolbarItems

UITextView becomeFirstResponder crash with EXC bad access

I have a UIViewController with UITextView in the storyboard I assigned it with strong variable.The first time when I present the ViewController using story board segue there is no crash but when I dismiss the ViewController and again present the ViewController for the second time after [UITextView becomeFirstResponder] is called the app crashes and shows the following error
[UIViewController _responderWindow]: message sent to deallocated instance
I have assigned delegate to the ViewController in which the TextView is added and implemented all the delegate methods
I tried calling this method [UITextview becomeFirstResponder] after a delay
but no use it was still crashing
I tried profiling with zombie template the error is in UIKit
this is the response I am getting when the app crashed if enable zombies in the product scheme
[ReviewViewController _responderWindow]: message sent to deallocated instance 0x14ed3a2a0
this is the code I am using to present the viewcontroller which has the textview
[_promotionsViewControllerDelegate performSegueWithIdentifier:#"testing" sender:nil];
textview viewcontroller
- (void)viewDidLoad {
[super viewDidLoad];
_reviewTextView.delegate = self;
[_reviewTextView becomeFirstResponder];
}
this is the back trace error log when i run the application without zombies
* thread #1: tid = 0x758d4, 0x000000019271bbd0 libobjc.A.dylib`objc_msgSend + 16, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x174697260)
frame #0: 0x000000019271bbd0 libobjc.A.dylib`objc_msgSend + 16
frame #1: 0x000000018675f96c UIKit`-[UIView(Internal) _firstResponder] + 24
frame #2: 0x000000018675f940 UIKit`-[UIResponder isFirstResponder] + 32
frame #3: 0x0000000186e96ed8 UIKit`-[UITextView _keyboardDidShow:] + 32
frame #4: 0x0000000181f58ae4 CoreFoundation`__CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
frame #5: 0x0000000181e97220 CoreFoundation`_CFXNotificationPost + 2060
frame #6: 0x0000000182d96cc0 Foundation`-[NSNotificationCenter postNotificationName:object:userInfo:] + 72
frame #7: 0x0000000186e5f458 UIKit`-[UIInputWindowController postEndNotifications:withInfo:] + 580
frame #8: 0x0000000186e60c7c UIKit`__77-[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:]_block_invoke595 + 396
frame #9: 0x000000018679c0a4 UIKit`-[UIViewAnimationBlockDelegate _didEndBlockAnimation:finished:context:] + 408
frame #10: 0x000000018679bc0c UIKit`-[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 188
frame #11: 0x000000018679bb14 UIKit`-[UIViewAnimationState animationDidStop:finished:] + 104
frame #12: 0x00000001860c0f64 QuartzCore`CA::Layer::run_animation_callbacks(void*) + 296
frame #13: 0x00000001002b4df0 libdispatch.dylib`_dispatch_client_callout + 16
frame #14: 0x00000001002b975c libdispatch.dylib`_dispatch_main_queue_callback_4CF + 1056
frame #15: 0x0000000181f69fa4 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
frame #16: 0x0000000181f6804c CoreFoundation`__CFRunLoopRun + 1492
frame #17: 0x0000000181e950a4 CoreFoundation`CFRunLoopRunSpecific + 396
frame #18: 0x000000018b0375a4 GraphicsServices`GSEventRunModal + 168
frame #19: 0x00000001867ca3c0 UIKit`UIApplicationMain + 1488
* frame #20: 0x00000001000c1030 WineDisciples`main(argc=1, argv=0x000000016fd83a58) + 116 at main.m:14
frame #21: 0x0000000192d76a08 libdyld.dylib`start + 4
I Kindly request you to help me on this thanks..
Did you check your Identifier in inspector window similar to given in code (i.e. "testing" )?
Also in your .h file had you given the . If not then add it.
Maybe is too late, but I had the same problem and the solution was to send remove from superview in the dealloc method of my vwc
deinit {
self.commentTextView.delegate = nil
self.commentTextView.removeFromSuperview()
}

UIResponder(Static) _setFirstResponder: break

I have no any idea about tihs break,
Can you provide some clues for me, Any help would be appreciated.
thread #1: tid = 0x29927, 0x37c47b66 libobjc.A.dylib`objc_msgSend + 6, queue = 'com.apple.main-thread, stop reason = EXC_BAD_ACCESS (code=1, address=0x8000000c)
frame #0: 0x37c47b66 libobjc.A.dylib`objc_msgSend + 6
frame #1: 0x3012cce0 UIKit`-[UIResponder(Static) _setFirstResponder:] + 44
frame #2: 0x3012cce0 UIKit`-[UIResponder(Static) _setFirstResponder:] + 44
frame #3: 0x3012cce0 UIKit`-[UIResponder(Static) _setFirstResponder:] + 44
frame #4: 0x30118732 UIKit`-[UIResponder resignFirstResponder] + 250
frame #5: 0x3027bb68 UIKit`-[UIWebDocumentView resignFirstResponder] + 232
frame #6: 0x3027ba7c UIKit`-[UIWebBrowserView resignFirstResponder] + 124
frame #7: 0x30072504 UIKit`-[UIView(Hierarchy) _willMoveToWindow:] + 660
frame #8: 0x30072d74 UIKit`__85-[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:withObject:copySublayers:]_block_invoke + 76
frame #9: 0x30072ca2 UIKit`-[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:withObject:copySublayers:] + 378
frame #10: 0x30072d94 UIKit`__85-[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:withObject:copySublayers:]_block_invoke + 108
frame #11: 0x30072ca2 UIKit`-[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:withObject:copySublayers:] + 378
frame #12: 0x3007fb9a UIKit`-[UIView(Internal) _addSubview:positioned:relativeTo:] + 418
frame #13: 0x3014490c UIKit`-[UITableViewCell _addSubview:positioned:relativeTo:] + 88
frame #14: 0x3007f9f2 UIKit`-[UIView(Hierarchy) addSubview:] + 30
frame #15: 0x00068b2e 4399Games1.0`-[SJGameDetailController tableView:cellForRowAtIndexPath:](self=0x162c8e00, _cmd=0x3065c3d7, tableView=0x163b0c00, indexPath=0x19172ab0) + 6478 at SJGameDetailController.m:1515
frame #16: 0x301a6314 UIKit`-[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 408
frame #17: 0x3014e6cc UIKit`-[UITableView _updateVisibleCellsNow:] + 1800
frame #18: 0x3014def0 UIKit`-[UITableView layoutSubviews] + 184
frame #19: 0x30074352 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 346
frame #20: 0x2fcfa942 QuartzCore`-[CALayer layoutSublayers] + 142
frame #21: 0x2fcf6166 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 350
frame #22: 0x2fcf5ff8 QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16
frame #23: 0x2fcf5a0c QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 228
frame #24: 0x2fcf581e QuartzCore`CA::Transaction::commit() + 314
frame #25: 0x2fcef54c QuartzCore`CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 56
frame #26: 0x2d8baf68 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
frame #27: 0x2d8b88f6 CoreFoundation`__CFRunLoopDoObservers + 286
frame #28: 0x2d8b8c42 CoreFoundation`__CFRunLoopRun + 738
frame #29: 0x2d823470 CoreFoundation`CFRunLoopRunSpecific + 524
frame #30: 0x2d823252 CoreFoundation`CFRunLoopRunInMode + 106
frame #31: 0x3255d2ea GraphicsServices`GSEventRunModal + 138
frame #32: 0x300d8844 UIKit`UIApplicationMain + 1136
frame #33: 0x000498c8 4399Games1.0`main(argc=1, argv=0x27dbecfc) + 116 at main.m:16
Rayfleck is on right track.
When you want to close the keyboard due to a textfield somewhere in your view, use following & let iOS handle the responder chain
[self.view endEditing:YES];
This is really helpful when there is a table in the view and the table has textfields everywhere.
From Apple Doc:
- (BOOL)endEditing:(BOOL)force
Description
Causes the view (or one of its embedded text fields) to resign the first responder status.
This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign.
Parameters
force
Specify YES to force the first responder to resign, regardless of whether it wants to do so.
Returns
YES if the view resigned the first responder status or NO if it did not.
I had the exact same problem with a UITextField. When I replaced
[myTextField resignFirstResponder];
with
[myTextField endEditing:YES];
the crashes stopped.

Strange Crash when dismissing view controller, auto-layout to blame?

I am experiencing a very strange crash, here is the backtrace.
* thread #1: tid = 0x2403, 0x3379516c CoreFoundation`CFHash + 8, stop reason = EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xdefe)
frame #0: 0x3379516c CoreFoundation`CFHash + 8
frame #1: 0x33797a9c CoreFoundation`CFBasicHashRemoveValue + 1408
frame #2: 0x337974ee CoreFoundation`CFDictionaryRemoveValue + 166
frame #3: 0x3420988e Foundation`-[NSISEngine removeConstraintWithMarker:] + 562
frame #4: 0x34211dbe Foundation`-[NSLayoutConstraint _removeFromEngine:] + 230
frame #5: 0x35a954ec UIKit`-[UIView(UIConstraintBasedLayout) _layoutEngine_willRemoveLayoutConstraint:] + 44
frame #6: 0x358488fc UIKit`__48-[UIScrollView _setAutomaticContentConstraints:]_block_invoke_0 + 148
frame #7: 0x34208882 Foundation`-[NSISEngine withAutomaticOptimizationDisabled:] + 166
frame #8: 0x35848838 UIKit`-[UIScrollView _setAutomaticContentConstraints:] + 116
frame #9: 0x35848e6c UIKit`-[UIScrollView _rememberDependentConstraint:] + 112
frame #10: 0x35a9e3ae UIKit`___updateViewDependenciesForConstraint_block_invoke_0 + 30
frame #11: 0x35a954ba UIKit`_updateViewDependenciesForConstraint + 202
frame #12: 0x35a953da UIKit`-[UIView(UIConstraintBasedLayout) _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 154
frame #13: 0x35a95534 UIKit`-[UIView(UIConstraintBasedLayout) _tryToAddConstraintWithoutUpdatingConstraintsArray:roundingAdjustment:mutuallyExclusiveConstraints:] + 36
frame #14: 0x3567c2e0 UIKit`-[UIView(Internal) _didMoveFromWindow:toWindow:] + 376
frame #15: 0x356d34fe UIKit`-[UIScrollView _didMoveFromWindow:toWindow:] + 50
frame #16: 0x3567c5c6 UIKit`-[UIView(Internal) _didMoveFromWindow:toWindow:] + 1118
frame #17: 0x35676e52 UIKit`-[UIView(Hierarchy) _postMovedFromSuperview:] + 138
frame #18: 0x3565e7dc UIKit`-[UIView(Internal) _addSubview:positioned:relativeTo:] + 1300
frame #19: 0x3565e2c2 UIKit`-[UIView(Hierarchy) addSubview:] + 30
frame #20: 0x356f68e8 UIKit`-[UITransitionView transition:fromView:toView:removeFromView:] + 972
frame #21: 0x35937618 UIKit`__91-[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:]_block_invoke_0238 + 388
frame #22: 0x357499b8 UIKit`-[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:] + 4824
frame #23: 0x357b9814 UIKit`-[UIViewController _dismissViewControllerWithTransition:from:completion:] + 1708
frame #24: 0x357057c4 UIKit`-[UIViewController dismissViewControllerWithTransition:completion:] + 912
frame #25: 0x000ccd40 Capture`-[INFTagSearchViewController cancelButtonTouched:](self=0x1f09ed50, _cmd=0x001b9d5f, sender=0x1e0265c0) + 76 at INFTagSearchViewController.m:48
frame #26: 0x357470c4 UIKit`-[UIApplication sendAction:to:from:forEvent:] + 72
frame #27: 0x35747076 UIKit`-[UIApplication sendAction:toTarget:fromSender:forEvent:] + 30
frame #28: 0x35747054 UIKit`-[UIControl sendAction:to:forEvent:] + 44
frame #29: 0x3574690a UIKit`-[UIControl(Internal) _sendActionsForEvents:withEvent:] + 502
frame #30: 0x35746e00 UIKit`-[UIControl touchesEnded:withEvent:] + 488
frame #31: 0x3566f5f0 UIKit`-[UIWindow _sendTouchesForEvent:] + 524
frame #32: 0x3565c800 UIKit`-[UIApplication sendEvent:] + 380
frame #33: 0x3565c11a UIKit`_UIApplicationHandleEvent + 6154
frame #34: 0x373655a2 GraphicsServices`_PurpleEventCallback + 590
frame #35: 0x373651d2 GraphicsServices`PurpleEventCallback + 34
frame #36: 0x33829172 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 34
frame #37: 0x33829116 CoreFoundation`__CFRunLoopDoSource1 + 138
frame #38: 0x33827f98 CoreFoundation`__CFRunLoopRun + 1384
frame #39: 0x3379aebc CoreFoundation`CFRunLoopRunSpecific + 356
frame #40: 0x3379ad48 CoreFoundation`CFRunLoopRunInMode + 104
frame #41: 0x373642ea GraphicsServices`GSEventRunModal + 74
frame #42: 0x356b0300 UIKit`UIApplicationMain + 1120
frame #43: 0x000a297c Capture`main(argc=1, argv=0x2fd60cfc) + 116 at main.m:16
frame #44: 0x3bb2bb20 libdyld.dylib`start + 4
You will notice frame 25 is the only call my code makes, and it is cancelButtonTouched which looks like.
- (IBAction)cancelButtonTouched:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
I am at a loss as to what is going on, I've never had an issue like this before, based on the system calls it appears to be an auto-layout issue. I verified that viewWillApper is called on the presenting view controller before the crash occurs, so I assume it is an issuing during layout of the presenting view.
Any ideas on how to narrow this issue down to a specific layout contstraint? Or any other ideas?
This is reproduceable on some devices, where as on others it rarely if ever happens. So it is intermittent.
Update
Screenshot of breakpoint.
Update 2
So it is definitely an issue with dismissing to the presentingViewController, dismissing two levels deep ([self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil]) works fine, but obviously not the desired result, I only want to go back 1 level. This further leads me to believe there is an issue "re-laying" out the view.
I seem to remember something like this, though not sure it was the exact same thing. In my case, it was caused by calling addSubview: (or insertSubview:...) with a view which already was a subview. While normally I think UIView deals with that, with auto layout it seems that it's possible for some associated information to be added elsewhere twice, and there can be a crash when trying to clean out that associated information. In my case, the solution was to ensure that I only added subviews once, and this crash (or my similar one anyways) went away.
Based on idea of Carl Lindberg I prepared and used the following iOS category and I have no crash anymore:
UIView+AddSubviewWithRemovingFromParent.h
#import <UIKit/UIKit.h>
#interface UIView (AddSubviewWithRemovingFromParent)
- (void)addSubviewWithRemovingFromParent:(UIView *)view;
#end
UIView+AddSubviewWithRemovingFromParent.m
#import "UIView+AddSubviewWithRemovingFromParent.h"
#implementation UIView (AddSubviewWithRemovingFromParent)
- (void)addSubviewWithRemovingFromParent:(UIView *)view {
if (view.superview != nil) {
[view removeFromSuperview];
}
[self addSubview:view];
}
#end
now you can use the addSubviewWithRemovingFromParent method to add the subView instead of addSubview method like this:
UITableViewCell *cell = [[UITableViewCell alloc] init];
[cell.contentView addSubviewWithRemovingFromParent:<viewToAdd>];
To sum it up:
find all references of addSubView in your controllers where the app
is crashing
import the category UIView+AddSubviewWithRemovingFromParent.h
use method addSubviewWithRemovingFromParent instead of addSubView
In my case this crash happened when I had a view controller in a storyboard but loaded the view contents in from a NIB with the same view controller as the file owner. This normally works fine, but I had the view outlet set in both the storyboard and the NIB, which triggered the iOS6 "add subview twice" bug. By clearing the view outlet in the NIB all was fixed.
I had / have the same problem.
The same problem occurs to me, when i present two view controllers modally above each other and want to return to the first one.
Disabling autolayout for the first presented view only solves the issue for me.
In your case self.presentingviewcontroller
Unfortunately i wasn't able to narrow it down further.

View controller uses one nib fine but crashes with another

I'm working on a universal app and the iPhone version works just fine. I created a new nib for iPad and set the files owner to be the viewcontroller class. The I connected all the IBOutlets and IBActions. When I run the app on an iPad (and in the iPad simulator) it crashes when I touch any of the controls. I tried setting break points in the IBActions for each but it doesn't even make it to those before the crash happens. What's going on?
Backtrace:
* thread #1: tid = 0x1f03, 0x0189809b libobjc.A.dylibobjc_msgSend + 15, stop reason = EXC_BAD_ACCESS (code=2, address=0x15)
frame #0: 0x0189809b libobjc.A.dylibobjc_msgSend + 15
frame #1: 0x0020f14e UIKit-[UIApplication sendAction:to:from:forEvent:] + 96
frame #2: 0x0020f0e6 UIKit-[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
frame #3: 0x002b5ade UIKit-[UIControl sendAction:to:forEvent:] + 66
frame #4: 0x002b5fa7 UIKit-[UIControl(Internal) _sendActionsForEvents:withEvent:] + 503
frame #5: 0x002b5b13 UIKit-[UIControl sendActionsForControlEvents:] + 48
frame #6: 0x00304462 UIKit-[UISegmentedControl _setSelectedSegmentIndex:notify:] + 684
frame #7: 0x00305dd0 UIKit-[UISegmentedControl touchesBegan:withEvent:] + 1026
frame #8: 0x002342cf UIKit-[UIWindow sendTouchesForEvent:] + 272
frame #9: 0x002345e6 UIKit-[UIWindow sendEvent:] + 273
frame #10: 0x0021adc4 UIKit-[UIApplication sendEvent:] + 464
frame #11: 0x0020e634 UIKit_UIApplicationHandleEvent + 8196
frame #12: 0x021c0ef5 GraphicsServicesPurpleEventCallback + 1274
frame #13: 0x012a1195 CoreFoundation__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
frame #14: 0x01205ff2 CoreFoundation_CFRunLoopDoSource1 + 146
frame #15: 0x012048da CoreFoundation__CFRunLoopRun + 2218
frame #16: 0x01203d84 CoreFoundationCFRunLoopRunSpecific + 212
frame #17: 0x01203c9b CoreFoundationCFRunLoopRunInMode + 123
frame #18: 0x021bf7d8 GraphicsServicesGSEventRunModal + 190
frame #19: 0x021bf88a GraphicsServicesGSEventRun + 103
frame #20: 0x0020c626 UIKitUIApplicationMain + 1163
frame #21: 0x00001d8d gravity`main + 141 at main.m:16
Turns out the view controller wasn't being retained due to ARC. So when I went back to send messages to it it crashed.

Resources