问题
I have the following code:
/// Creates Alerts on screen for user.
func notifyUser(title: String, message: String) -> Void
{
let alert = UIAlertController(title: title,
message: message,
preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "OK",
style: .Cancel, handler: nil)
alert.addAction(cancelAction)
UIApplication.sharedApplication().keyWindow?.rootViewController!.presentViewController(alert, animated: true,
completion: nil)
}
Which shows the following Alert:
I would prefer the Alert to appear for maybe 1-2 seconds and auto dismiss without having to click ok or dismiss. Is this possible?
回答1:
Yes it's completely possible, I think the @Duncan C approach will work very well and it's self explanatory, so I going to explain you in code the @Duncan approach and another approach is using delays with the Grand Central Dispatch(GCD).
First Approach: Using the
NSTimer
class
// set the UIAlerController property
var alert: UIAlertController!
func notifyUser(title: String, message: String, timeToDissapear: Int) -> Void
{
alert = UIAlertController(title: title,
message: message,
preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "OK",
style: .Cancel, handler: nil)
alert.addAction(cancelAction)
UIApplication.sharedApplication().keyWindow?.rootViewController!.presentViewController(alert, animated: true,
completion: nil)
// setting the NSTimer to close the alert after timeToDissapear seconds.
_ = NSTimer.scheduledTimerWithTimeInterval(Double(timeToDissapear), target: self, selector: Selector("dismissAlert"), userInfo: nil, repeats: false)
}
Second Approach: Using GCD
// set the UIAlerController property
var alert: UIAlertController!
func notifyUser(title: String, message: String, timeToDissapear: Int) -> Void
{
alert = UIAlertController(title: title,
message: message,
preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "OK",
style: .Cancel, handler: nil)
alert.addAction(cancelAction)
UIApplication.sharedApplication().keyWindow?.rootViewController!.presentViewController(alert, animated: true,
completion: nil)
// Delay the dismissal by timeToDissapear seconds
let delay = Double(timeToDissapear) * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue()) { [weak self] in
self!.alert.dismissViewControllerAnimated(true, completion: nil)
}
}
And then you can call it in anywhere you want like in the following way :
self.notifyUser("Hello", message: "World", timeToDissapear: 3)
I hope this help you.
回答2:
Here Is the code for Swift 4 Please Refer...Thank you
let alert = UIAlertController(title: "Success", message: "Record Updated Successfully", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
switch action.style{
case .default:
print("default")
case .cancel:
print("cancel")
case .destructive:
print("destructive")
}}))
self.present(alert, animated: true, completion: nil)
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
alert.dismiss(animated: true, completion: nil)
}
回答3:
Sure. A UIAlertController
is a special type of UIViewController
. You're displaying it using presentViewController:animated:completion:
. Just save a pointer to the UIAlertController
into an instance variable, start a timer, and when the timer fires, call dismissViewControllerAnimated:completion:
. You might want to get rid of the OK button action in that case, and if you leave the OK button you'll need to test and make sure your code works if you click OK before the timer fires.
回答4:
Try this code:
var timer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "dismissAlert", userInfo: nil, repeats: true)
func dismissAlert()
{
// Dismiss the alert from here
alert.dismissViewControllerAnimated(false, completion: nil)
}
I tried it and this worked fine. You can set the timer inside
scheduledTimerWithTimeInterval
where the current time is set to 5.0 seconds
回答5:
func alert2 (_ dictKey: String){
if self.presentedViewController == nil {
let alertController = UIAlertController(title: nil, message: dictKey, preferredStyle: .alert )
alertController.addAction(UIAlertAction(title: "START AGAIN", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction!) in self.returnToStart()}))
alertController.addAction(UIAlertAction(title: "REQUEST PIN", style: UIAlertActionStyle.default, handler:{(action:UIAlertAction!) in self.pinCreate()
self.dismiss(animated: false, completion: nil)//This dismisses the alert
}))
self.present(alertController, animated: true,completion: nil)
}
}
This proved to be a simple solution
来源:https://stackoverflow.com/questions/35094360/uialert-in-swift-that-automatically-disappears