Multiple UIAlertControllers in iOS
The following is a way to display multiple warnings:
UIAlertController *av = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:kAlertOk
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
}];
[av addAction:cancelAction];
UIWindow *alertWindow = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
alertWindow.rootViewController = [[UIViewController alloc]init];
alertWindow.windowLevel = UIWindowLevelAlert + 1;
[alertWindow makeKeyAndVisible];
[alertWindow.rootViewController presentViewController:av animated:YES completion:nil];
source to share
You can keep track of the list of warnings displayed in the view controller as an instance variable like
NSMutableArray *alertsToShow;
You can imagine the first UIAlertController and add a UIAlertAction where you present the following warning (if applicable) with a recursive method:
- (void)showAlertIfNecessary {
if (alertsToShow.count == 0)
return;
NSString *alert = alertsToShow[0];
[alertsToShow removeObjectAtIndex:0];
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"Title"
message:alert
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
[self showAlertIfNecessary];
}];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
}
Please note that this can become very annoying for the user if they have to click a lot of messages. You may want to consider combining them into one message.
source to share
You can't show multiple alerts at the same time, and if you've done it before, you're misbehaving. Reimagine your interface.
You can easily present the warnings in turn, which is what you really need:
let alert = UIAlertController(title: "One", message: nil, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Next", style: .Default, handler: {
_ in
let alert2 = UIAlertController(title: "Two", message: nil, preferredStyle: .Alert)
alert2.addAction(UIAlertAction(title: "OK", style: .Cancel, handler:nil))
self.presentViewController(alert2, animated: true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
source to share
Swift version for @ Bejibun answer above:
let alertView = UIAlertController(title: "New Message", message: "Message Body", preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) in
alertView.dismissViewControllerAnimated(true, completion: nil)
}))
let alertWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
alertWindow.rootViewController = UIViewController()
alertWindow.windowLevel = UIWindowLevelAlert + 1
alertWindow.makeKeyAndVisible()
alertWindow.rootViewController?.presentViewController(alertView, animated: true, completion: nil)
source to share
I think I am very late for this, but post anyway as it might be helpful for someone looking for this, although Apple does not recommend multiple notifications, so they dropped this UIAlertView from the UIAlertController implementation.
I created a subclass of AQAlertAction for UIAlertAction. You can use it for stunning warnings, the usage is the same as you use UIAlertAction. All you have to do is import the AQMutiAlertFramework into your project, or you can include the class as well (see the sample project for this). Internally, It uses a binary semaphore to daze Alerts until the user processes the action associated with the current alert being displayed. Let me know if this works for you.
source to share
You can use JSAlertView which handles UIAlertView and UIAlertController API.
It handles multiple warnings issued at the same time very well. It also provides very simple methods for displaying simple warnings.
source to share
You can find Top Most View Controller using this function.
func topViewController(_ base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(presented)
}
return base
}
Present UIAlertController
Using this method via. as,
topViewController()?.present(alertController, animated: true, completion: nil)
Method Information: topViewController()
Find Top Most Presented View Controller,
UIAlertController
Dinner Class UIViewController
.
first is UIAlertController
open normally in the top presented view controller, try opening the second UIAlertController
one and then topViewController()
given the first warning. therefore no missed UIAlertController
.
source to share