UIAlertView及UIAlertController在不同系统版本中的自适配

本文介绍了一个自定义类ZLAlertView,用于解决iOS 8.0之后UIAlertView与UIAlertController之间的兼容性问题。该类可根据系统版本自动选择合适的弹框方式,并提供了简单易用的接口。

iOS8.0之后,苹果新推出了一种弹框提醒UIAlertController来替代UIAlertView,这也为我们挖了一些坑。

比如我就曾与到在8.3系统上,在键盘弹出正在输入的模式下,此时如果点返回按钮的情况下,使用UIAlertView弹框提示用户是否确定返回(为UINavigationController模式下),此时当返回到上个界面之后,键盘会从左至右一闪而过。

还有就是比如我们在一个imageView上添加了弹框事件,提示内容为“查看大图,取消”,此时如果我们在如下点击方法内去把大图添加到[UIApplication sharedApplication].keyWindow上面,那么会在alertView消失的时候,连同我们的大图一起从window上移除。  这情况就是在8.0之上系统使用UIAlertView 出现的。


资源链接:http://download.csdn.net/detail/longitachi/9246645


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

那么我们需要自定义一个类,去实现在不同的系统下,自动选择使用哪种控件去完成所需操作

该类.h如下

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

NS_ASSUME_NONNULL_BEGIN

typedef void (^ClickAction)();

@interface ZLAlertView : NSObject

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *message;

/**
 * @param title    标题
 * @param message  提示内容
 */
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message;

- (void)setTitle:(NSString *)title message:(NSString *)message;

/**
 * @brief 添加按钮及事件,多个按钮便多次调用,按钮按照添加顺序显示
 */
- (void)addBtnTitle:(NSString *)title action:(ClickAction)action;

/**
 * @brief 显示提示框
 */
- (void)showAlertWithSender:(UIViewController *)sender;

NS_ASSUME_NONNULL_END

@end

.m实现如下
#import "ZLAlertView.h"

#define iOS8_0 [[[UIDevice currentDevice] systemVersion] doubleValue] >= 8.0

@interface ZLAlertView () <UIAlertViewDelegate>
{
    NSMutableArray *_arrayTitles;
    NSMutableArray *_arrayActions;
}

//iOS8.0之后会使用UIAlertController,所以需要使用调用该类的ViewController
@property (nonatomic, weak) UIViewController *sender;

@end

@implementation ZLAlertView

//重写该方法,保证该对象不会被释放,如果被释放,iOS8以下的UIAlertView的回调时候会崩溃
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    static ZLAlertView *_shareAlertView = nil;
    dispatch_once(&onceToken, ^{
        if (_shareAlertView == nil) {
            _shareAlertView = [super allocWithZone:zone];
        }
    });
    return _shareAlertView;
}

- (instancetype)init
{
    if (self = [super init]) {
        _arrayTitles = [NSMutableArray array];
        _arrayActions = [NSMutableArray array];
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message
{
    if ([self init]) {
        _title = title;
        _message = message;
    }
    return self;
}

- (void)setTitle:(NSString *)title message:(NSString *)message
{
    _title = title;
    _message = message;
}

- (void)addBtnTitle:(NSString *)title action:(ClickAction)action
{
    [_arrayTitles addObject:title];
    [_arrayActions addObject:action];
}

- (void)showAlertWithSender:(UIViewController *)sender
{
    if (_arrayTitles.count == 0) {
        return;
    }
    self.sender = sender;
    if (iOS8_0) {
        [self showAlertController];
    } else {
        [self showAlertView];
    }
}

- (void)showAlertController
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:_title message:_message preferredStyle:UIAlertControllerStyleAlert];
    for (int i = 0; i < _arrayTitles.count; i++) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:_arrayTitles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            ClickAction ac = _arrayActions[i];
            ac();
        }];
        [alert addAction:action];
    }
    if (_sender) {
        [_sender showDetailViewController:alert sender:nil];
    }
}

- (void)showAlertView
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_title message:_message delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    for (NSString *title in _arrayTitles) {
        [alert addButtonWithTitle:title];
    }
    
    [alert show];
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    ClickAction action = _arrayActions[buttonIndex];
    action();
}

@end

使用方法如下

#import "ZLAlertView.h"
    ZLAlertView *alert = [[ZLAlertView alloc] initWithTitle:@"提示" message:@"alertView"];
    [alert addBtnTitle:@"first" action:^{
        NSLog(@"click first");
    }];
    [alert addBtnTitle:@"second" action:^{
        NSLog(@"click second");
    }];
    [alert showAlertWithSender:self];




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值