私记-AlertController封装
2018-02-06 14:18
162 查看
AlertController.h文件
AlertController.m文件
-
#import <Foundation/Foundation.h> @interface AlertController : NSObject /** alert @param title 标题 @param message 内容 @param actionTitles 按钮数组 @param controller 显示控制器 @param clickAtIndex 点击按钮的block */ +(void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message ActionTitles:(NSArray *) actionTitles controller:(id) controller handler:(void(^)(NSInteger index)) clickAtIndex; /** alert @param title 标题 @param message 内容 @param text 当前field的内容 @param placeholder field的占位符 @param actionTitles 按钮数组 @param controller 显示控制器 @param clickAtIndex 点击按钮的block */ +(void)showAlertFieldWithTitle:(NSString *)title message:(NSString *)message fieldText:(NSString *)text fieldplaceholder:(NSString *)placeholder ActionTitles:(NSArray *) actionTitles controller:(id) controller handler:(void(^)(NSString *text, NSInteger index)) clickAtIndex; /** alert @param title 标题 @param message 内容 @param actionTitles 按钮数组 @param controller 显示控制器 @param clickAtIndex 点击按钮的block */ +(void)showAlertSheetWithTitle:(NSString *)title message:(NSString *)message ActionTitles:(NSArray *) actionTitles controller:(id) controller handler:(void(^)(NSInteger index)) clickAtIndex; /** alert @param title 标题 @param message 内容 @param alignment 内容显示样式(对齐样式) @param actionTitle 单个按钮标题(只适合设置单个按钮) @param controller 显示控制器 */ +(void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message messageAlignment:(NSTextAlignment) alignment ActionTitle:(NSString *) actionTitle controller:(id) controller; @end
AlertController.m文件
-
#import "AlertController.h" @implementation AlertController +(void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message ActionTitles:(NSArray *) actionTitles controller:(id) controller handler:(void(^)(NSInteger index)) clickAtIndex { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; for (NSInteger i = 0; i<actionTitles.count; i++) { UIAlertAction *action = [UIAlertAction actionWithTitle:actionTitles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { if (clickAtIndex) { clickAtIndex(i); } }]; [alertVc addAction:action]; } [controller presentViewController:alertVc animated:YES completion:nil]; }); } +(void)showAlertFieldWithTitle:(NSString *)title message:(NSString *)message fieldText:(NSString *)text fieldplaceholder:(NSString *)placeholder ActionTitles:(NSArray *) actionTitles controller:(id) controller handler:(void(^)(NSString *text, NSInteger index)) clickAtIndex { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; [alertVc addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = placeholder; textField.text = text; }]; for (NSInteger i = 0; i<actionTitles.count; i++) { UIAlertAction *action = [UIAlertAction actionWithTitle:actionTitles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { if (clickAtIndex) { clickAtIndex(alertVc.textFields[0].text,i); } }]; [alertVc addAction:action]; } [controller presentViewController:alertVc animated:YES completion:nil]; }); } +(void)showAlertSheetWithTitle:(NSString *)title message:(NSString *)message ActionTitles:(NSArray *) actionTitles controller:(id) controller handler:(void(^)(NSInteger index)) clickAtIndex { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet]; for (NSInteger i = 0; i<actionTitles.count; i++) { UIAlertAction *action = [UIAlertAction actionWithTitle:actionTitles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { if (clickAtIndex) { clickAtIndex(i); } }]; [alertVc addAction:action]; } UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alertVc addAction:cancel]; [controller presentViewController:alertVc animated:YES completion:nil]; }); } +(void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message messageAlignment:(NSTextAlignment) alignment ActionTitle:(NSString *) actionTitle controller:(id) controller { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; UIView *subView1 = alertVc.view.subviews[0]; UIView *subView2 = subView1.subviews[0]; UIView *subView3 = subView2.subviews[0]; UIView *subView4 = subView3.subviews[0]; UIView *subView5 = subView4.subviews[0]; //取title和message: UILabel *title = subView5.subviews[0]; UILabel *message = subView5.subviews[1]; message.textAlignment = alignment; if (actionTitle) { UIAlertAction *cancel = [UIAlertAction actionWithTitle:actionTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alertVc addAction:cancel]; } [controller presentViewController:alertVc animated:YES completion:nil]; }); } @end
相关文章推荐
- alertController的封装(在TYAlertController基础上封装)
- AlertController工具类的封装,让AlertController变得更简单
- iOS (封装)一句话调用系统的alertView和alertController
- OC-类的声明与实现、方法的声明与实现、面向对象的三大特性之封装
- pthread简单封装 线程退出
- 一个封装好的nofication类
- 一个小菜鸡封装继承的制作披萨
- BaseDao的封装
- 封装时所用的四个魔术方法
- WCF 服务端异常封装
- Html5 APP开发及封装----Html5及Web开发教程地址-萌太叔
- okHttp的封装
- Swift实现封装PopMenu菜单,可在屏幕任意位置弹出
- unity简单封装的网络请求
- java使用httpclient封装post请求和get的请求
- 封装的JS弹出框
- 微信公众帐号开发教程第6篇-消息及消息处理工具的封装
- Socket编程实践(11) --epoll原理与封装
- iOS (UIButton封装)仿糯米首页缩放“按钮”效果
- Javascript 面向对象编程(一):封装