iOS_UIAlertController
ViewController.m文件 #import "ViewController.h" @interface ViewController () // @property (nonatomic, strong) UIAlertController *alert; @property (nonatomic, strong) UIAlertController *actionSheet; @property (nonatomic, weak) IBOutlet UIButton *showAlertBtn; @property (nonatomic, weak) IBOutlet UIButton *showActionSheetBtn; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //创建UIAlertController self.alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"消息" preferredStyle:UIAlertControllerStyleAlert]; //动作 UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { NSLog(@"点击了取消"); }]; //动作 UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSLog(@"点击了确定"); }]; //动作 UIAlertAction *act3 = [UIAlertAction actionWithTitle:@"销毁" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { NSLog(@"点击了销毁"); }]; //将所有动作添加到控制器中 [self.alert addAction:act1]; [self.alert addAction:act2]; [self.alert addAction:act3]; self.actionSheet = [UIAlertController alertControllerWithTitle:@"标题" message:@"消息" preferredStyle:UIAlertControllerStyleActionSheet]; // 在action sheet中,UIAlertActionStyleCancel不起作用 UIAlertAction *act4 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSLog(@"点击了取消"); }]; UIAlertAction *act5 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSLog(@"点击了确定"); }]; UIAlertAction *act6 = [UIAlertAction actionWithTitle:@"销毁" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { NSLog(@"点击了销毁"); }]; [self.actionSheet addAction:act4]; [self.actionSheet addAction:act5]; [self.actionSheet addAction:act6]; } //显示单击事件 - (IBAction)showAlert:(id)sender { [self presentViewController:self.alert animated:YES completion:^{ }]; } - (IBAction)showActionSheet:(id)sender { [self presentViewController:self.actionSheet animated:YES completion:^{ }]; } @end
运行效果图:
iOS8之后用UIAlertController代替了UIAlertView,所以每次有需要弹窗的时候,都需要先判断系统,最近在做的项目中弹窗较多,如果每次都判断,真是太麻烦了,索性对UIAlertController和UIAlertView进行的封装了,封装在一个工具类中,在工具类中就对系统进行判断,然后在你需要弹窗的界面直接调用这个工具类的方法就可以了,减少了代码的耦合.
UIAlertTool.h文件 #import <Foundation/Foundation.h> @interface UIAlertTool : NSObject -(void)showAlertView:(UIViewController *)viewController :(NSString *)title :(NSString *)message :(NSString *)cancelButtonTitle :(NSString *)otherButtonTitle :(void (^)())confirm :(void (^)())cancle; ;@end
UIAlertTool.m文件 #import "UIAlertTool.h" #define IAIOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)#import "UIAlertTool.h"typedef void (^confirm)(); typedef void (^cancle)(); @interface UIAlertTool() { confirm confirmParam; canclecancleParam;} @end @implementation UIAlertTool -(void)showAlertView:(UIViewController *)viewController :(NSString *)title :(NSString *)message :(NSString *)cancelButtonTitle :(NSString *)otherButtonTitle :(void (^)())confirm :(void (^)())cancle { confirmParam=confirm; cancleParam=cancle; if (IAIOS8) { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; // Create the actions. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { cancle(); }]; UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { confirm(); }]; // Add the actions. [alertController addAction:cancelAction]; [alertController addAction:otherAction]; [viewController presentViewController:alertController animated:YES completion:nil]; } else { UIAlertView *TitleAlert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:otherButtonTitle otherButtonTitles:cancelButtonTitle,nil]; [TitleAlert show]; } } -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex==0) { confirmParam(); } else{ cancleParam(); } } @end