How can I display a pop-up message in Swift that disappears after 3 seconds or can be canceled by the user immediately?

In my quick application, I have a one-button UIViewController.

This button calls up a function that calls up a popup that disappears after 3 seconds. In addition, after this time, he displays a message on the console. The code for this function is as follows:

func showAlertMsg(title: String, message: String){ let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert) self.presentViewController(alertController, animated: true, completion: nil) let delay = 3.0 * Double(NSEC_PER_SEC) let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay)) dispatch_after(time, dispatch_get_main_queue(), { alertController.dismissViewControllerAnimated(true, completion: nil) print("popup disappeared") }) } 

This works great, but I would like to make some improvements. I wanted to add a button there that immediately cancels this popup and then does not display a message in the console. Is there a way to display such a popup for the user? In addition, is there a way to show the counter with the number of remaining seconds in this pop-up message that shows how much time is left until the pop-up window disappears?

+5
source share
2 answers

You can use NSTimer to decrease the counter, update the warning view and cancel the warning view when the counter reaches 0. This code is adapted from my Objective-C response

 class ViewController: UIViewController { var alertController: UIAlertController? var alertTimer: NSTimer? var remainingTime = 0 var baseMessage: String? override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) self.showAlertMsg("Test Alert", message: "This will disappear in ", time: 5) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func showAlertMsg(title: String, message: String, time: Int) { guard (self.alertController == nil) else { print("Alert already displayed") return } self.baseMessage = message self.remainingTime = time self.alertController = UIAlertController(title: title, message: self.alertMessage(), preferredStyle: .Alert) let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in print("Alert was cancelled") self.alertController=nil; self.alertTimer?.invalidate() self.alertTimer=nil } self.alertController!.addAction(cancelAction) self.alertTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ViewController.countDown), userInfo: nil, repeats: true) self.presentViewController(self.alertController!, animated: true, completion: nil) } func countDown() { self.remainingTime -= 1 if (self.remainingTime < 0) { self.alertTimer?.invalidate() self.alertTimer = nil self.alertController!.dismissViewControllerAnimated(true, completion: { self.alertController = nil }) } else { self.alertController!.message = self.alertMessage() } } func alertMessage() -> String { var message="" if let baseMessage=self.baseMessage { message=baseMessage+" " } return(message+"\(self.remainingTime)") } } 
+5
source

I know this does not directly answer your question, but have you thought to use MBProgressHUD SCLAlertView ? They both offer features that allow you to display a warning that disappears after a certain time. SCLAlertView allows the user to immediately cancel where MBROgressHUD is not. If you want more information on how to implement them, let me know so that I can add more information!

0
source

Source: https://habr.com/ru/post/1247464/


All Articles