简体   繁体   中英

iOS App Crash when presenting UIAlertController

I am getting a Thread 1: EXC_BAD_ACCESS when I try to present an UIAlertController on my ViewController.

This is my code:

let controller = UIAlertController(title: "bob", message: nil, preferredStyle: .actionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
controller.addAction(cancelAction)
DispatchQueue.main.async {
    self.present(controller, animated: false, completion: nil)
}

I have also tried replacing the UIAlertController with a blank UIViewController() and I'm getting a very similar stack trace with the crash in the same place.

This is the stack trace.

#0  0x00007fff2b13578d in CA::Layer::retain_parent(CA::Transaction*) const ()
#1  0x00007fff2b1455b9 in CALayerGetSuperlayer ()
#2  0x00007fff485813a8 in -[UIView(UIKitManual) superview] ()
#3  0x00007fff485bf1aa in -[_UITintColorVisitor _visitView:] ()
#4  0x00007fff485beb1f in _UIViewVisitorRecursivelyEntertainDescendingVisitors ()
#5  0x00007fff485bec06 in _UIViewVisitorRecursivelyEntertainDescendingVisitors ()
#6  0x00007fff485bec06 in _UIViewVisitorRecursivelyEntertainDescendingVisitors ()
#7  0x00007fff485bec06 in _UIViewVisitorRecursivelyEntertainDescendingVisitors ()
#8  0x00007fff485bec06 in _UIViewVisitorRecursivelyEntertainDescendingVisitors ()
#9  0x00007fff485bec06 in _UIViewVisitorRecursivelyEntertainDescendingVisitors ()
#10 0x00007fff485bec06 in _UIViewVisitorRecursivelyEntertainDescendingVisitors ()
#11 0x00007fff485bec06 in _UIViewVisitorRecursivelyEntertainDescendingVisitors ()
#12 0x00007fff485bec06 in _UIViewVisitorRecursivelyEntertainDescendingVisitors ()
#13 0x00007fff485bec06 in _UIViewVisitorRecursivelyEntertainDescendingVisitors ()
#14 0x00007fff485bec06 in _UIViewVisitorRecursivelyEntertainDescendingVisitors ()
#15 0x00007fff485bec06 in _UIViewVisitorRecursivelyEntertainDescendingVisitors ()
#16 0x00007fff485bec06 in _UIViewVisitorRecursivelyEntertainDescendingVisitors ()
#17 0x00007fff485be6d2 in +[_UIViewVisitor _startTraversalOfVisitor:withView:] ()
#18 0x00007fff4854d9db in -[UIView _dispatchTintColorVisitorWithReasons:] ()
#19 0x00007fff4792522a in -[UIPresentationController _enableOcclusion:] ()
#20 0x00007fff47a42124 in -[_UIViewControllerTransitionCoordinator _applyBlocks:releaseBlocks:] ()
#21 0x00007fff47a3e446 in -[_UIViewControllerTransitionContext __runAlongsideAnimations] ()
#22 0x00007fff4856a44f in __63+[UIView(Animation) _setAlongsideAnimations:toRunByEndOfBlock:]_block_invoke ()
#23 0x00007fff4853b7c0 in -[UIViewAnimationState _runAlongsideAnimations] ()
#24 0x00007fff4853a50a in -[UIViewAnimationState pop] ()
#25 0x00007fff485378ce in +[UIViewAnimationState popAnimationState] ()
#26 0x00007fff4856be60 in +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] ()
#27 0x00007fff4856c231 in +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:] ()
#28 0x00007fff4763b3a3 in -[UIAlertControllerVisualStyleActionSheet animateAlertControllerView:ofAlertController:forPresentation:inContainerView:descendantOfContainerView:duration:completionBlock:] ()
#29 0x00007fff47627ac3 in -[_UIAlertControllerAnimatedTransitioning _animateTransition:completionBlock:] ()
#30 0x00007fff4762748f in -[_UIAlertControllerAnimatedTransitioning animateTransition:] ()
#31 0x00007fff47a42cf5 in ___UIViewControllerTransitioningRunCustomTransition_block_invoke_2 ()
#32 0x00007fff47b5af7e in +[UIInputResponderController _pinInputViewsForInputResponderController:onBehalfOfResponder:duringBlock:] ()
#33 0x00007fff47a42c89 in ___UIViewControllerTransitioningRunCustomTransition_block_invoke.645 ()
#34 0x00007fff4856a351 in +[UIView(Animation) _setAlongsideAnimations:toRunByEndOfBlock:] ()
#35 0x00007fff47a42b0f in _UIViewControllerTransitioningRunCustomTransition ()
#36 0x00007fff47924740 in __56-[UIPresentationController runTransitionForCurrentState]_block_invoke.465 ()
#37 0x00007fff4809be5b in _runAfterCACommitDeferredBlocks ()
#38 0x00007fff4808c7b4 in _cleanUpAfterCAFlushAndRunDeferredBlocks ()
#39 0x00007fff480bc3a9 in _afterCACommitHandler ()
#40 0x00007fff23bd3867 in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ ()
#41 0x00007fff23bce2fe in __CFRunLoopDoObservers ()
#42 0x00007fff23bce97a in __CFRunLoopRun ()
#43 0x00007fff23bce066 in CFRunLoopRunSpecific ()
#44 0x00007fff384c0bb0 in GSEventRunModal ()
#45 0x00007fff48092d4d in UIApplicationMain ()
#46 0x000000010c50ca8b in main at /Users/stephenfurlani/Developer/winterfell/Winterfell/AppDelegate.swift:21
#47 0x00007fff5227ec25 in start ()

I have no idea what's going on. UIAlertController works fine in other places in my application. It crashes on my iPhone 11 Pro Max 13.1.3, but works fine on my iPhone 11 13.1.3. It crashes if I run everything on the main thread in the async{} block.

I will also note that it crashes on the simulator for iPhone 11 Pro Max but not the simulator for iPhone 11.

My VC Hierarchy is as follows:

TabBar -tab-> Navigation -root-> TableView -show-> DetailView

The detail view is what is presenting the UIAlertController

TabBar -tab-> Navigation -root-> TableView -show-> DetailView -show-> SubDetailView

I can present an AlertController from the subdetail page as shown above.


I tried to create a small project to highlight the error, but it works just fine. X( https://github.com/sfurlani/crashingMenuBar


None of the answers to these questions help:
* display UIAlert will crash the app on present?
* App crash when presenting UIAlertController
* Swift UIAlertController -> ActionSheet iPad iOS8 Crashes

The code you wrote works well on iphone but for iPad action sheet doesn't appears like iphone. it appears from the UI Element where you did Tap. so for action sheet to work on iPad

       let controller = UIAlertController(title: "bob", message: nil, preferredStyle: .actionSheet)
      let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
      controller.addAction(cancelAction)
  if let presenter = controller.popoverPresentationController {
          // From Bar Button
       presenter.barButtonItem = yourBarButtonItem
  // Comment the above line if you are tapping on some UIElement other than barButton
          presenter.sourceView = yourUIView
     presenter.sourceRect = yourUIView.bounds
  }

      DispatchQueue.main.async {
          self.present(controller, animated: false, completion: nil)
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM