实现对UIAlertController和UIAlertView判断系统后的简单封装
2014-12-18 15:30
323 查看
iOS8之后用UIAlertController代替了UIAlertView,所以每次有需要弹窗的时候,都需要先判断系统,最近在做的项目中弹窗较多,如果每次都判断,真是太麻烦了,索性对UIAlertController和UIAlertView进行的封装了,封装在一个工具类中,在工具类中就对系统进行判断,然后在你需要弹窗的界面直接调用这个工具类的方法就可以了,减少了代码的耦合.
这个工具类其实也封装的特别简单,因为都是用的系统的,分享出来给大家参考下:
首先是.h文件
只有这么一个简单的方法 把你需要在弹窗中显示的内容以参数的形式传入就可以了
然后是.m文件的实现
你在弹窗中点击确定或者取消要实现的功能写在block里面传入就OK了.SO easy吧!
这个工具类其实也封装的特别简单,因为都是用的系统的,分享出来给大家参考下:
首先是.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
你在弹窗中点击确定或者取消要实现的功能写在block里面传入就OK了.SO easy吧!
相关文章推荐
- 实现对UIAlertController和UIAlertView判断系统后的简单封装
- 实现对UIAlertController和UIAlertView判断系统后的简单封装
- [IOS]对UITableView的简单封装,实现了下拉放大头部图片的效果
- iOS系统自带的 **UIAlertView** 以及 屏幕自动旋转的实现
- Android-万能 Adapter 封装(实现textview,imageView 的简单封装,封装 jar 包)
- 模仿系统方法自定义UIAlertView的实现和代理
- UICollectionView实现的图片的多选效果(本人已封装好,简单操作)
- iOS 简单的实现手机号正误的判断(Warning: Attempt to present... whose view is not in the window hierarchy!
- iOS将系统的UIAlertView代理方式改为Block回调封装
- webview 加载h5页面,播放视频+全屏,实现简单封装
- Swift 使用CollectionView 实现图片轮播封装就是这样简单
- UITableView_5-团购简单实现,nib封装实现,模型封装实现
- [iOS/Swift]简单实现跟系统一样的毛玻璃效果的View,可在上面加控件,可移动
- 系统调用的简单封装类实现
- 基于逻辑运算的简单权限系统(原理,设计,实现) VBS 版
- 防止模拟键盘一类工具操作自己的系统的“简单实现”
- 简单文件系统的实现
- AJAX简单实现新闻拖放系统
- 地磅称量系统之(53)在封装对象的类库中实现包括IDataErrorInfo接口提供的所有方法和并且扩展对异常具有添加和删除功能的基本业务对象基类
- 基于逻辑运算的简单权限系统(实现) JS 版