Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismissing UIAlertController automatically

My application has a UILongPressGestureRecognizer, which display an alert with UIAlertController when the user touches two elements on the screen. Currently, the user must press the OK button in order to dismiss the alert, however I would like to dismiss the alert after 0.5 seconds automatically, so the user doesn't compromise his interaction with the app.

Is there any way to do that?

like image 612
Cris R Avatar asked Nov 28 '14 03:11

Cris R


People also ask

How do you dismiss an alert controller in Swift?

You can dismiss the alert by calling dismissViewControllerAnimated method on alertController object.

How do you dismiss an alert with click on outside of the alert IOS?

Step 1 − Open Xcode and create a single view application and name it UIAlertSample. So basically, when we tap on the button an alert will be displayed, when the user taps outside the alert the alert will be dismissing.


1 Answers

You can achieve the automatic dismiss of alert view by using dispatch_after of GCD.

Try below code :

    let delay = 0.5 * Double(NSEC_PER_SEC)
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
    dispatch_after(time, dispatch_get_main_queue()) { () -> Void in
        self.dismissPopover()
    }

Here, dismissCategoryPopover() is a custom method that will be called automatically after 0.5 seconds to dismiss the alert view.

like image 120
Abhishek Avatar answered Oct 02 '22 19:10

Abhishek