在我的应用程序中,有些情况可能会出现多个警报.但是在iOS8 UIAlertview转向UIAlertController时,我无法显示多个警报,因为您无法同时显示两个或更多控制器.
如何使用UIAlertController实现此目的?
以下是显示多个alertControllers的方法:
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];
Run Code Online (Sandbox Code Playgroud)
您可以跟踪警报列表以作为实例变量显示在您的视图控制器中,例如
NSMutableArray *alertsToShow;
Run Code Online (Sandbox Code Playgroud)
您可以显示第一个 UIAlertController,并添加一个 UIAlertAction,您可以在其中使用类似递归的方法显示下一个警报(如果适用):
- (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];
}
Run Code Online (Sandbox Code Playgroud)
请注意,如果他/她需要点击大量消息,这可能会让用户非常恼火。您可以考虑将它们合并为一条消息。
您不能同时显示多个警报,如果您以前这样做过,那么您的行为就很糟糕。重新思考你的界面。
您可以轻松地连续显示警报,这就是您真正需要的:
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8284 次 |
| 最近记录: |