runtime应用--将UIAlertView改造成UIAlertController

本文旨在将UIAlertView改造,使UIAlertView的用法类似UIAlertController,使得代码逻辑更加清晰化,方便阅读和维护.


目录
一 UIAlertView和UIAlertController的简介
二 具体代码实现
三 使用范例
四 适用范围


一 UIAlertView和UIAlertController的简介

1 UIAlertView简介
1.1 UIAlertView 主要接口简介

/**
 UIAlertView的默认初始化方法

 @param title 标题
 @param message 提示语
 @param delegate 代理<UIAlertViewDelegate>
 @param cancelButtonTitle 取消按钮的文字
 @param otherButtonTitles 其他按钮的文字
 @return UIAlertView 实例
 */
- (instancetype)initWithTitle:(nullable NSString *)title message:(nullable NSString *)message delegate:(nullable id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(nullable NSString *)cancelButtonTitle otherButtonTitles:(nullable NSString *)otherButtonTitles, ... ;
// Alert view style - defaults to UIAlertViewStyleDefault
@property(nonatomic,assign) UIAlertViewStyle alertViewStyle

1.2 UIAlertViewDelegate接口:

// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 9_0);

1.3 用法示例:

//显示alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"选取头像" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"从相册中选取",@"照相", nil];
[alert show];
//监听点击按钮的代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:
            NSLog(@"点击了取消");
            break;
        case 1:
            NSLog(@"点击了从相册中选取");
            break;
        case 2:
            NSLog(@"点击了照相");
            break;
        default:
            break;
    }
}

注意: buttonIndex为0 一定对应"取消"按钮, buttonIndex然后按照otherButtonTitles的顺序从1开始 依次+1;

2 UIAlertController简介
2.1 UIAlertController主要接口介绍

/**
 默认初始化方法

 @param title 标题
 @param message 提示信息
 @param preferredStyle 弹出方式
 @return UIAlertController实例
 */
+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;
/*
   向alert添加按钮和事件
*/
- (void)addAction:(UIAlertAction *)action;

2.2 UIAlertAction接口介绍

/**
 UIAlertAction默认初始化方法
 
 @param title 按钮显示文字
 @param style 按钮的类型
 @param handler 点击按钮触发的事件Block
 @return UIAlertAction实例
 */
+ (instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler;

2.3 用法示例

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"提示信息" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"点击了确定后干什么写在这里");
}];

UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"点击了取消后干什么写在这里,会默认关闭alert");
}];

[alert addAction:action1];
[alert addAction:action2];
[self presentViewController:alert animated:YES completion:nil];

二 代码实现:

接下来是本文的重点,简单说明就是用UIAlertController的方式使用UIAlertView.
3.1 依赖类:LQAlertAction----功能类似UIAlertAction代码如下:

LQAlertAction.h
#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger, LQAlertActionStyle) {
    LQAlertActionStyleDefault = 0,
    LQAlertActionStyleCancel,
    LQAlertActionStyleDestructive
};
@class LQAlertAction;
typedef void(^LQAlertActionHandler)(LQAlertAction *action);

@interface LQAlertAction : NSObject
/**
 LQAlertAction默认初始化方法

 @param title 标题
 @param style 样式(目前没有作用,留作后用)
 @param handler 事件block
 @return LQAlertAction实例
 */
+ (instancetype)alertActionWithTitle:(nullable NSString *)title style:(LQAlertActionStyle)style handler:(nullable LQAlertActionHandler)handler;

@property (nullable ,copy) LQAlertActionHandler handler;
@property (nullable, nonatomic, readonly) NSString *title;
@property (nonatomic, readonly) LQAlertActionStyle style;
@property (nonatomic, getter=isEnabled) BOOL enabled;

@end

LQAlertAction.m

#import "LQAlertAction.h"

@interface LQAlertAction ()
@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) LQAlertActionStyle style;
@end

@implementation LQAlertAction
+(instancetype)alertActionWithTitle:(NSString *)title style:(LQAlertActionStyle)style handler:(void (^)(LQAlertAction *))handler{
    LQAlertAction *action = [[LQAlertAction alloc]init];
    action.handler = handler;
    action.title = title;
    action.style = style;
    return action;
}
- (NSString *)title{
    if (_title == nil) {
        return @"";
    }
    return _title;
}
- (LQAlertActionStyle)style{
    return _style;
}
@end

3.2 给UIAlertView增加一个分类:
UIAlertView+Extention.h
#import <UIKit/UIKit.h>
#import "LQAlertAction.h"
@interface UIAlertView (Extention)
@property (nonatomic, strong) NSArray *actions;

/**
 UIAlertView初始化方法

 @param actions LQAlertAction的数组,默认会有一个取消按钮
 @param title 标题
 @param message 提示信息
 */
+(void)showAlertWithActions:(NSArray *)actions title:(NSString *)title message:(NSString *)message;
@end

UIAlertView+Extention.m
#import "UIAlertView+Extention.h"
#import <objc/runtime.h>
@interface UIAlertView ()<UIAlertViewDelegate>

@end

@implementation UIAlertView (Extention)
static NSArray *_actions = nil;

+(void)showAlertWithActions:(NSArray *)actions title:(NSString *)title message:(NSString *)message{
    return [[self alloc] showAlertWithActions:actions title:title message:message];
}

-(void)showAlertWithActions:(NSArray *)actions title:(NSString *)title message:(NSString *)message{
    
    NSMutableArray *otherButtonTitle = [[NSMutableArray alloc]init];
    
    NSMutableArray *tempActionArray = [[NSMutableArray alloc] init];//整理action的顺序 0对应的是取消按钮
    LQAlertAction *cancel = [self cancelActionWithTitle:@"取消"];
    [tempActionArray addObject:cancel];
    for (LQAlertAction *action in actions) {
        if (action.style == LQAlertActionStyleCancel) {
            //            cancelButtonTitle = action.title;
            cancel = [self cancelActionWithTitle:action.title];
            break;
        } else {
            [otherButtonTitle addObject:action.title];
            [tempActionArray addObject:action];
        }
        
    }
    
    self.actions = tempActionArray.copy;
    UIAlertView *alert = [self initWithTitle:title message:message delegate:self cancelButtonTitle:cancel.title otherButtonTitles:nil];
    for (NSString *str in otherButtonTitle) {
        [alert addButtonWithTitle:str];
    }
    
    [alert show];
}
- (LQAlertAction *)cancelActionWithTitle:(NSString *)title{
    LQAlertAction *action = [LQAlertAction alertActionWithTitle:title style:LQAlertActionStyleCancel handler:^(LQAlertAction *action) {
        
    }];
    return action;
}


- (void)setActions:(NSArray *)actions{
    objc_setAssociatedObject(self, &_actions, actions, OBJC_ASSOCIATION_RETAIN);
}
- (NSArray *)actions{
    
    return objc_getAssociatedObject(self, &_actions);
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex>self.actions.count-1) {
        return;
    }
    if (self.actions[buttonIndex]) {
        LQAlertAction *action = self.actions[buttonIndex];
        if (action.handler) {
            action.handler(action);
        }
    }
}
-(void)setAlertViewStyle:(UIAlertViewStyle)alertViewStyle{
    
}

三 使用范例

代码如下:

LQAlertAction *action1 = [LQAlertAction alertActionWithTitle:@"从相册选取" style:LQAlertActionStyleDefault handler:^(LQAlertAction *action) {
    NSLog(@"打开相册");
}];

LQAlertAction *action2 = [LQAlertAction alertActionWithTitle:@"照相" style:LQAlertActionStyleDefault handler:^(LQAlertAction *action) {
    NSLog(@"打开相机");
}];

LQAlertAction *cancle = [LQAlertAction alertActionWithTitle:@"取消" style:LQAlertActionStyleCancel handler:nil];
[UIAlertView showAlertWithActions:@[action1,action2,cancle] title:@"选取头像" message:@"信息内容"];

四 使用范围

目前仅支持UIAlertViewStyle = UIAlertViewStyleDefault的情况,后续视情况增加新的支持

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,519评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,842评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,544评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,742评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,646评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,027评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,513评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,169评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,324评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,268评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,299评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,996评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,591评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,667评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,911评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,288评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,871评论 2 341

推荐阅读更多精彩内容