UIAlertView和UIAlertController的封装
来源:互联网 发布:c语言怎么判断闰年 编辑:程序博客网 时间:2024/06/05 00:42
在iOS开发过程会经常使用到提示框(UIAlertView),iOS 8之后UIAlertView便被UIAlertController所代替,下面我们就对两者进行相应的封装方便使用,这边把UIAlertView的代理封装成block回调方便使用,废话不多说上代码才是硬道理。
.h文件
#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>#pragma mark - UIAlertController回调typedef void(^AlertViewBlock)(NSInteger buttonTag);#pragma mark - UIAlertView回调// 点击的下标回调typedef void (^AlertViewClickedWithIndex)(NSInteger index);// 点击确定按钮回调typedef void (^AlertViewOkClicked)();// 点击取消按钮回调typedef void (^AlertViewCancelClicked)();@interface XL_UtilsAlertController : NSObject#pragma mark - 使用 UIAlertController/** * @brief 弹出提示框,AlertController可变参数版 * * @param title 标题 * @param message 提示信息 * @param cancelTitle 取消按钮文字 * @param confirmTitle 确定按钮文字 * @param confirmHandler 确定回调 * @param cancleHandler 取消回调 */+(void)showAlertControllerWithTitle:(NSString *)title mesasge:(NSString *)message cancelTitle:(NSString *)cancelTitle confirmTitle:(NSString *)confirmTitle confirmHandler:(void(^)(UIAlertAction *action))confirmHandler cancleHandler:(void(^)(UIAlertAction *action))cancleHandler;#pragma mark - 使用 UIAlertController ---Sheet/** * 创建菜单(Sheet 可变参数版) * * @param title 标题 * @param message 提示内容 * @param cancelTitle 取消按钮(默认@“取消”) * @param confirm 点击按钮的回调 * @param buttonTitles 按钮 */+ (void)showSheetController:(NSString *)title message:(NSString *)message cancelTitle:(NSString *)cancelTitle buttonTitles:(NSArray *)buttonTitles confirm:(AlertViewBlock)confirm;#pragma mark - 使用 UIAlertView/** * @brief 弹出提示框,点击返回点击下标 * * @param title 标题 * @param msg 提示信息 * @param cancelTitle 取消按钮文字 * @param okTitle 确定按钮文字 * @param otherTitleArray 其他按钮文字 * @param handle 点击回调 */+ (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)msg cancleButtonTitle:(NSString *)cancelTitle okButtonTitle:(NSString *)okTitle otherButtonTitleArray:(NSArray*)otherTitleArray clickHandle:(AlertViewClickedWithIndex) handle;/** * @brief 弹出提示框只有确定和取消按钮 * * @param title 标题 * @param msg 提示信息 * @param cancelTitle 取消按钮文字 * @param okTitle 确定按钮文字 * @param okHandle 点击确定回调 * @param cancelClickHandle 点击取消回调 */+ (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)msg cancleButtonTitle:(NSString *)cancelTitle okButtonTitle:(NSString *)okTitle okClickHandle:(AlertViewOkClicked)okHandle cancelClickHandle:(AlertViewCancelClicked)cancelClickHandle;/** * @brief 弹出框,没有回调. * * @param title 标题 * @param msg 提示信息 * @param cancelTitle 取消按钮的文字 * @param okTitle 确定按钮的文字 * @param otherTitleArray 其他按钮文字 */+ (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)msg cancleButtonTitle:(NSString *)cancelTitle okButtonTitle:(NSString *)okTitle otherButtonTitleArray:(NSArray*)otherTitleArray;@end
.m文件
#import "XL_UtilsAlertController.h"#import <objc/runtime.h>#define RootVC [UIApplication sharedApplication].delegate.window.rootViewControllerconst char *KAlertViewIndexBlock = "AlertViewIndexBlock";const char *KAlertViewOkBlock = "AlertViewOkBlock";const char *KAlertViewCancelBlock = "AlertViewCancelBlock";@interface UIAlertView(XL_UtilsAlertController)@property (nonatomic,copy)AlertViewClickedWithIndex indexBlock;@property (nonatomic,copy)AlertViewOkClicked okBlock;@property (nonatomic,copy)AlertViewCancelClicked cancelBlock;@end@implementation UIAlertView(XL_UtilsAlertController)- (void)setIndexBlock:(AlertViewClickedWithIndex)indexBlock{ objc_setAssociatedObject(self, KAlertViewIndexBlock, indexBlock, OBJC_ASSOCIATION_COPY);}- (AlertViewClickedWithIndex)indexBlock{ return objc_getAssociatedObject(self, KAlertViewIndexBlock);}- (void)setOkBlock:(AlertViewOkClicked)okBlock{ objc_setAssociatedObject(self, KAlertViewOkBlock, okBlock, OBJC_ASSOCIATION_COPY);}- (AlertViewOkClicked)okBlock{ return objc_getAssociatedObject(self, KAlertViewOkBlock);}- (void)setCancelBlock:(AlertViewCancelClicked)cancelBlock{ objc_setAssociatedObject(self, KAlertViewCancelBlock, cancelBlock, OBJC_ASSOCIATION_COPY);}-(AlertViewCancelClicked)cancelBlock{ return objc_getAssociatedObject(self, KAlertViewCancelBlock);}@end@interface XL_UtilsAlertController ()<UIAlertViewDelegate>@end@implementation XL_UtilsAlertController#pragma mark -AlertController+(void)showAlertControllerWithTitle:(NSString *)title mesasge:(NSString *)message cancelTitle:(NSString *)cancelTitle confirmTitle:(NSString *)confirmTitle confirmHandler:(void(^)(UIAlertAction *action))confirmHandler cancleHandler:(void(^)(UIAlertAction *action))cancleHandler{ UIAlertController *alertController=[UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; if (cancelTitle.length > 0 || (confirmTitle.length == 0 && cancelTitle.length == 0)) { NSString *title = cancelTitle.length > 0?cancelTitle:@"取消"; UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleCancel handler:cancleHandler]; [alertController addAction:cancleAction]; } if (confirmTitle.length > 0) { UIAlertAction *confirmAction=[UIAlertAction actionWithTitle:confirmTitle style:UIAlertActionStyleDefault handler:confirmHandler]; [alertController addAction:confirmAction]; } [RootVC presentViewController:alertController animated:YES completion:nil];}+ (void)showSheetController:(NSString *)title message:(NSString *)message cancelTitle:(NSString *)cancelTitle buttonTitles:(NSArray *)buttonTitles confirm:(AlertViewBlock)confirm{ UIAlertController *sheet = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet]; NSString *ctitle = cancelTitle.length > 0?cancelTitle:@"取消"; UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:ctitle style:UIAlertActionStyleCancel handler:nil]; [sheet addAction:cancleAction]; if (buttonTitles.count > 0) { for (NSInteger i = 0; i < buttonTitles.count; i++) { UIAlertAction *action = [UIAlertAction actionWithTitle:buttonTitles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { if (confirm)confirm(i); }]; [sheet addAction:action]; } } [RootVC presentViewController:sheet animated:YES completion:nil];}#pragma mark -AlertViewW+ (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)msg cancleButtonTitle:(NSString *)cancelTitle okButtonTitle:(NSString *)okTitle otherButtonTitleArray:(NSArray*)otherTitleArray clickHandle:(AlertViewClickedWithIndex) handle{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:handle ? self : nil cancelButtonTitle:cancelTitle otherButtonTitles:okTitle, nil]; if (handle) { alert.indexBlock = handle; } for (NSString *otherTitle in otherTitleArray) { [alert addButtonWithTitle:otherTitle]; } [alert show];}+ (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)msg cancleButtonTitle:(NSString *)cancelTitle okButtonTitle:(NSString *)okTitle okClickHandle:(AlertViewOkClicked)okHandle cancelClickHandle:(AlertViewCancelClicked)cancelClickHandle{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:okTitle,nil]; if (okHandle) { alert.okBlock = okHandle; } if (cancelTitle) { alert.cancelBlock = cancelClickHandle; } [alert show];}+ (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)msg cancleButtonTitle:(NSString *)cancelTitle okButtonTitle:(NSString *)okTitle otherButtonTitleArray:(NSArray*)otherTitleArray{ [self showAlertViewWithTitle:title message:msg cancleButtonTitle:cancelTitle okButtonTitle:okTitle otherButtonTitleArray:otherTitleArray clickHandle:nil];}#pragma mark - UIAlertViewDelegate+ (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if (alertView.indexBlock) { alertView.indexBlock(buttonIndex); }else{ if (buttonIndex == 0) {//取消 if (alertView.cancelBlock) { alertView.cancelBlock(); } }else{//确定 if (alertView.okBlock) { alertView.okBlock(); } } }}@end
0 0
- UIAlertView和UIAlertController的封装
- UIAlertController 和 UIAlertView的使用
- UIAlertView和UIAlertController的使用
- 实现对UIAlertController和UIAlertView判断系统后的简单封装
- 实现对UIAlertController和UIAlertView判断系统后的简单封装
- 实现对UIAlertController和UIAlertView判断系统后的简单封装
- UIAlertView 和UIAlertController二合一
- UIAlertView 和 UIAlertController
- UIAlertController和UIAlertView
- iOS 更加优雅便捷的UIAlertView/UIAlertController封装使用
- iOS 更加优雅便捷的UIAlertView/UIAlertController封装使用
- UIAlertController,取代UIAlertView和UIActionSheet
- iOS 8 引入的 UIActionSheet 和 UIAlertView 的替代品 - UIAlertController
- UIAlertController的用法以及和其与UIAlertView的区别
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet)
- iOS8 UIAlertView 和 UIActionSheet 的新用法 UIAlertController详解
- 【UI进阶】UIAlertController替代UIAlertView和UIActionSheet
- IOS UIAlertView和UIAlertController学习记录
- tesseract-OCR字库训练
- 解决iometer在linux和windows连通,出现windows界面卡住问题
- LeetCode专栏L007_Reverse_Integer
- “No JSON object could be decoded”处理方法
- HTML5新增结构标签
- UIAlertView和UIAlertController的封装
- 某公司T+M测试题
- QRCode 生成二维码
- 软件测试的定义与分类
- bindService的时候出现Service Intent must be explicit
- IFIX,VB读写脚本,PDB
- HibernateDaoSupport详解
- SQL中的Update Select
- PhpStorm最新版注册