在iOS 8中,UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一种模块化替换的方式来代替这两个的功能和作用。是使用对话框(alert)还是使用上拉菜单(action sheet),就取决于在创建控制器时,您是如何设置首选样式的。
但是如果项目要经常使用到提示框的时候,一直重复相同的代码也是一件头疼的事情。那么我们不妨封装一个UIAlertController工具类来提高我们的编码效率。
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface MKJTools : NSObject
+(UIAlertController *)createAlertWithTitle:(NSString *)title message:(NSString *)message preferred:(UIAlertControllerStyle)preferred confirmHandler:(void(^)(UIAlertAction *confirmAction))confirmHandler cancleHandler:(void(^)(UIAlertAction *cancleAction))cancleHandler;
@end
+(UIAlertController *)createAlertWithTitle:(NSString *)title message:(NSString *)message preferred:(UIAlertControllerStyle)preferred confirmHandler:(void (^)(UIAlertAction *))confirmHandler cancleHandler:(void (^)(UIAlertAction *))cancleHandler{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:preferred];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:confirmHandler];
[alertController addAction:confirmAction];
UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:cancleHandler];
[alertController addAction:cancleAction];
return alertController;
}
UIAlertController *alert = [MKJTools createAlertWithTitle:@"提示" message:@"这是个迷。。。" preferred:0 confirmHandler:^(UIAlertAction *confirmAction) {
//做你想做的事
} cancleHandler:^(UIAlertAction *cancleAction) {
}];
[self presentViewController:alert animated:YES completion:nil];