2

Is there a way to make the alert view disappear automatically.. after some seconds, without user action. Currently I have my code as follow, and it require user to press ok to disappear the alert dialog. I would like to show the alert and not have user intervention, and just have the alert disappear in few seconds. Thanks for any comments.

My code as below:

func showAlertController (message: String) {
    let alertController = UIAlertController(title: nil, message: message, preferredStyle: .Alert)
    alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
    presentViewController(alertController, animated: true, completion: nil)
}
Tedha
  • 507
  • 2
  • 6
  • 14

2 Answers2

2

You can delay anything with dispatch_after. For example, this would dismiss the alert view after 3 seconds.

let delayTime = dispatch_time(DISPATCH_TIME_NOW, 
                              Int64(3 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
    presentedViewController.dismissViewControllerAnimated(true, completion: nil);
}

You can also use @matt's awesome delay function.

Community
  • 1
  • 1
Daniel Larsson
  • 6,278
  • 5
  • 44
  • 82
0

Try the dismissViewControllerAnimated:completion: method

Kametrixom
  • 14,673
  • 7
  • 45
  • 62