实现对UIAlertController和UIAlertView判断系统后的简单封装
来源:互联网 发布:河南网络知识竞赛 编辑:程序博客网 时间:2024/05/22 05:16
实现对UIAlertController和UIAlertView判断系统后的简单封装
iOS8之后用UIAlertController代替了UIAlertView,所以每次有需要弹窗的时候,都需要先判断系统,最近在做的项目中弹窗较多,如果每次都判断,真是太麻烦了,索性对UIAlertController和UIAlertView进行的封装了,封装在一个工具类中,在工具类中就对系统进行判断,然后在你需要弹窗的界面直接调用这个工具类的方法就可以了,减少了代码的耦合.
这个工具类其实也封装的特别简单,因为都是用的系统的,分享出来给大家参考下:
首先是.h文件
[objc] view plaincopy- @interface UIAlertTool : NSObject- -(void)showAlertView:(UIViewController *)viewController :(NSString *)title :(NSString *)message :(NSString *)cancelButtonTitle :(NSString *)otherButtonTitle :(void (^)())confirm :(void (^)())cancle;- @end
只有这么一个简单的方法 把你需要在弹窗中显示的内容以参数的形式传入就可以了
然后是.m文件的实现
[objc] view plaincopy- #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
0 0
- 实现对UIAlertController和UIAlertView判断系统后的简单封装
- 实现对UIAlertController和UIAlertView判断系统后的简单封装
- 实现对UIAlertController和UIAlertView判断系统后的简单封装
- UIAlertView和UIAlertController的封装
- UIAlertController 和 UIAlertView的使用
- UIAlertView和UIAlertController的使用
- UIAlertView 和UIAlertController二合一
- UIAlertView 和 UIAlertController
- UIAlertController和UIAlertView
- iOS 更加优雅便捷的UIAlertView/UIAlertController封装使用
- iOS 更加优雅便捷的UIAlertView/UIAlertController封装使用
- UIAlertController的使用(ios9.0后代替UIAlertView与UIActionSheet)
- iOS 实现类似UIAlertView 和UIAlertController弹窗动画效果
- UIAlertController,取代UIAlertView和UIActionSheet
- iOS 8 引入的 UIActionSheet 和 UIAlertView 的替代品 - UIAlertController
- UIAlertController的用法以及和其与UIAlertView的区别
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet)
- iOS8 UIAlertView 和 UIActionSheet 的新用法 UIAlertController详解
- request_threaded_irq函数
- 两个NSData时间对比
- VMware8安装教程
- IE6中网页不显示iframe不显示问题
- Android开源项目分类汇总
- 实现对UIAlertController和UIAlertView判断系统后的简单封装
- 常用sql语句
- I2C和SPI总线对比
- ShareSDK的使用---实现三方登陆及分享
- 你为什么会离开游戏行业
- 树莓派硬件到,可以开搞了
- 【android】多个avtivity之间startActivityForResult小解
- mysql in 内排序
- MFC中ON_UPDATE_COMMAND_UI和ON_COMMAND消息区别