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
原创粉丝点击