Josh Josh - 1 year ago 129
Swift Question

iOS UIAlertController interrupting animation

Pretty simple situation. I have a custom UIViewController for filling in a form. When the user clicks the cancel button I want an alert to pop up; "are you sure you want to exit the form? You're edits won't be saved". If they hit okay, the VC does an animation and then closes.

I have the animation placed in the UIAlertAction handler closure. However, the animation happens instantly every time, I'm assuming this is because the UIAlertController animates when it dismisses (after the button is pressed) which then interrupts the animation of the VC.

Any ideas? Basically it seems as though the handler is being called as soon as the button is pressed; how can I call the handler AFTER the UIAlertController is finished dismissing.

Edit: Here is some sample code to illustrate the issue:

let refreshAlert = UIAlertController(title: "Delete draft?", message: "Are you sure you want to discard this draft? It will not be saved.", preferredStyle: UIAlertControllerStyle.Alert)

refreshAlert.addAction(UIAlertAction(title: "Delete", style: .Destructive, handler: { (action: UIAlertAction!) in
self.animationInPresentionVC() // This animation occurs instantly, it seems to be interrupted by the dismissing alert controller

Answer Source

maybe you can add some delay to execute your animationInPresentionVC():

refreshAlert.addAction(UIAlertAction(title: "Delete", style: .Destructive, handler: { (action: UIAlertAction!) in
    self.performSelector(#selector(**your animationInPresentionVC**), , withObject: nil,  afterDelay:0.3)