is there anyway to wait the user to press the button that dismiss the alertController in swift 3, by using DispatchQueue or something else?
-
1Take a look at - http://stackoverflow.com/questions/37801436/how-do-i-write-dispatch-after-gcd-in-swift-3 – dev gr Nov 10 '16 at 06:56
-
yes i kow about it, but i mean, after i presented the alertcontroller, is there anyway to wait (to make my code execute nothing ) till the user dismiss the alert ? – Anthony Shahine Nov 10 '16 at 07:01
-
1If you want to stop the execution of your code till user dismiss alert then add your execution code in `UIAlertAction`'s handler part. – Nirav D Nov 10 '16 at 07:05
-
@NiravD in fact, i add a normal button to the alert in order to dismiss it, so how could i add the handler to stop the execution of my code till user press this button ? – Anthony Shahine Nov 10 '16 at 07:11
-
1Put all your code that you want to execute later on that action handler. – Nirav D Nov 10 '16 at 07:12
-
@NiravD i couldn't put the next lines of code in the action handler for many reasons, but i need to execute them after the pressing of the button, because right now, these lines are executed directly after the showing of the alertcontroller – Anthony Shahine Nov 10 '16 at 07:27
-
@Anthony Then Make one function and put all that code inside function that you want to execute later and simply call that function in the action handler part. – Nirav D Nov 10 '16 at 07:29
-
@NiravD i'm still thinking about something like that : self.present(self.alertController, animated: true, completion : { () -> Void in ,and to make the dismiss inside of it, but it doesn't work, – Anthony Shahine Nov 10 '16 at 07:50
-
@Anthony Are you getting now? – Nirav D Nov 10 '16 at 08:35
3 Answers
Just move all the code you want to execute after the alert is dismissed into a separate method. When you're adding UIAlertActions
, make all the actions' handler
parameter call that method. This way, whichever button the user presses, your code will always be executed!

- 213,210
- 22
- 193
- 313
You mean something like this?
alertController.displayAndWaitUntilDismissed()
// This line is only reached after the alert controller is dismissed
print("Alert controller dismissed.")
Theoretically, yes, you could use a dispatch semaphore to block until the alert is dismissed. But it’s a bad idea – I can’t even think of a scenario where it would be acceptable. Simply accept that you have to deal with it asynchronously, by executing the desired code in the alert controller action.

- 102,279
- 44
- 260
- 354
You MUST NOT block the main thread and waiting for dismissing your alert so the only way is doing this asynchronously.
For instance you can use next common approach with UIViewControllerTransitioningDelegate
:
fileprivate var AssociatedObjectKey: UInt8 = 0
extension UIAlertController : UIViewControllerTransitioningDelegate {
var onDissmiss: (() -> Void)? {
set {
transitioningDelegate = newValue != nil ? self : nil
objc_setAssociatedObject(self, &AssociatedObjectKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
get {
return objc_getAssociatedObject(self, &AssociatedObjectKey) as? (() -> Void)
}
}
public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
onDissmiss?()
return nil
}
}
How to use:
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default)
alert.addAction(ok)
alert.onDissmiss = {
print("dissmiss")
}
present(alert, animated: true)

- 11,742
- 1
- 33
- 48