Move multiple UIAlertController

In some cases, my applications should display multiple Alert messages. Error messages are collected at startup and should be displayed to the user one at a time. When the first is recognized, the following should be presented. The problem is that they are all trying to execute at the same time, obviously. Is there any way to do this synchronously? Here is some code that just describes what I want to do:

var errors : [NSError]! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let error1 = NSError(domain: "Test1", code: 1, userInfo: [NSLocalizedFailureReasonErrorKey : "Test1 reason."]) let error2 = NSError(domain: "Test2", code: 2, userInfo: [NSLocalizedFailureReasonErrorKey : "Test2 reason."]) let error3 = NSError(domain: "Test3", code: 2, userInfo: [NSLocalizedFailureReasonErrorKey : "Test3 reason."]) errors = [error1, error2, error3] } override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) for error in errors { self.showAlert(error) } } func showAlert(error: NSError) { var alert = UIAlertController(title: error.domain, message: error.localizedDescription, preferredStyle: .Alert) alert.addAction(UIAlertAction(title: "OK", style: .Default, handler:nil)) self.presentViewController(alert, animated: true, completion: nil) } 
+5
source share
1 answer

You are almost there. Having a warning message buffer is the right idea. But instead of immediately displaying all warnings, you should move the showAlert() call to the showAlert() handler. Therefore, if one warning is rejected, the following will be shown.

Something like that:

 var errors : [NSError]! override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) let error1 = NSError(domain: "Test1", code: 1, userInfo: [NSLocalizedFailureReasonErrorKey : "Test1 reason."]) let error2 = NSError(domain: "Test2", code: 2, userInfo: [NSLocalizedFailureReasonErrorKey : "Test2 reason."]) let error3 = NSError(domain: "Test3", code: 2, userInfo: [NSLocalizedFailureReasonErrorKey : "Test3 reason."]) errors = [error1, error2, error3] showError() // show an alert if errors are queued } func showError() { if let error = errors.first { let alert = UIAlertController(title: error.domain, message: error.localizedDescription, preferredStyle: .Alert) let okayAction = UIAlertAction(title: "OK", style: .Default) { action in self.errors.removeAtIndex(0) // remove the message of the alert we have just dismissed self.showError() // show next alert if there are more errors queued } alert.addAction(okayAction) presentViewController(alert, animated: true, completion: nil) } else { println("All alerts shown") } } 

Tip: Firing a few warnings is very annoying. Perhaps you can create a special full-screen viewController that displays all error messages in a UITableView or something like that. This, of course, depends on the number of warning messages that a regular user will see. If it will be regularly more than three, I would use a modal UIViewController, which displays all messages at a glance.

+7
source

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


All Articles