实现对UIAlertController和UIAlertView判断系统后的简单封装
2016-01-20 17:02
417 查看
iOS8之后用UIAlertController代替了UIAlertView,所以每次有需要弹窗的时候,都需要先判断系统,最近在做的项目中弹窗较多,如果每次都判断,真是太麻烦了,索性对UIAlertController和UIAlertView进行的封装了,封装在一个工具类中,在工具类中就对系统进行判断,然后在你需要弹窗的界面直接调用这个工具类的方法就可以了,减少了代码的耦合.
这个工具类其实也封装的特别简单,因为都是用的系统的,分享出来给大家参考下:
首先是.h文件
只有这么一个简单的方法 把你需要在弹窗中显示的内容以参数的形式传入就可以了
然后是.m文件的实现
这个工具类其实也封装的特别简单,因为都是用的系统的,分享出来给大家参考下:
首先是.h文件
@interface UIAlertTool : NSObject -(void)showAlertView:(UIViewController *)viewController :(NSString *)title :(NSString *)message :(NSString *)cancelButtonTitle :(NSString *)otherButtonTitle :(void (^)())confirm :(void (^)())cancle;; @end
只有这么一个简单的方法 把你需要在弹窗中显示的内容以参数的形式传入就可以了
然后是.m文件的实现
#define IAIOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) #import "UIAlertTool.h" typedef void (^confirm)(); typedef void (^cancle)(); @interface UIAlertTool(){ confirm confirmParam; cancle cancleParam; } @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
相关文章推荐
- Animation动画详解(六)——ValueAnimator高级进阶(二)
- Zoho Recruit新界面赏析
- UIViewContentMode各类型效果
- ios9-NSLayoutAnchor和UILayoutGuide实现自动布局
- response.sendRedirect()与request.getRequestDispatcher().forward()区别
- UILabel - 自定义行间距,字间距及段间距[并动态调节高](转)
- 关于UINavigationBar遮挡UIView,UITableview的研究
- Codeforces 392C Yet Another Number Sequence 题解&代码
- easyui datagrid 跨页选择
- android Ui 卡顿检测
- kbuild是个什么鬼?
- SQL Server 2016 查询存储(Query Store)查询语句
- SQL Server 2016 查询存储(Query Store)查询语句
- 详释(常见UITextView输入之字数限制)之一固定长度
- 使用UIAlertController报错
- easyui控件与Activex控件遮挡问题
- UI基础__UICollectionView__瀑布流
- Android官方文档翻译-guide-储存选择
- workqueue 用法实例
- 优化RequireJS项目(合并与压缩)