iOS PSTAlertController 源码泛读

PSTAlertController 是为了能让用户从 iOS7开始兼容 UIAlertController

why UIAlertController?

iOS8 以后出的新类,目标就是为了替代 UIActionSheet
and UIAlertView
,接口也极其友好,容易创建,以controller的形式展示更方便点!

兼容iOS7 的痛苦,不能使用心得API 还好git上有这么多优秀的开源项目!

话说,为什么我们现在还要兼容iOS7呢??

PSTAlertController 其实也没有什么特别的地方通过判断能不能使用 UIAlertController 来创建的


- (BOOL)alertControllerAvailable {
    return [UIAlertController class] != nil; // iOS 8 and later.
}

以上就是判断创建的时候生成view的时候生成哪个

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(PSTAlertControllerStyle)preferredStyle {
    if ((self = [super init])) {
        _title = [title copy];
        _message = [message copy];
        _preferredStyle = preferredStyle;

        if ([self alertControllerAvailable]) {
            _alertController = [PSTExtendedAlertController alertControllerWithTitle:title message:message preferredStyle:(UIAlertControllerStyle)preferredStyle];
        } else {
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 80000
            if (preferredStyle == PSTAlertControllerStyleActionSheet) {
                _strongSheetStorage = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
            } else {
                _strongSheetStorage = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
            }
#endif
        }
    }
    return self;
}

创建的时候再根据能不能使用UIAlertController和类型来创建
UIAlertController 或者 UIActionSheet 或者 UIAlertView

- (UIAlertView *)alertView {
    return (UIAlertView *)(self.strongSheetStorage ?: self.weakSheetStorage);
}

- (UIActionSheet *)actionSheet {
    return (UIActionSheet *)(self.strongSheetStorage ?: self.weakSheetStorage);
}

获取alertView 或者 actionView

- (void)addAction:(PSTAlertAction *)action {
    NSAssert([action isKindOfClass:PSTAlertAction.class], @"Must be of type PSTAlertAction");

    action.alertController = self; // weakly connect

    self.actions = [[NSArray arrayWithArray:self.actions] arrayByAddingObject:action];

    if ([self alertControllerAvailable]) {
        __weak typeof (self) weakSelf = self;
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:action.title style:(UIAlertActionStyle)action.style handler:^(UIAlertAction *uiAction) {
            weakSelf.executedAlertAction = action;
            [action performAction];
        }];
        [self.alertController addAction:alertAction];
    } else {
        if (self.preferredStyle == PSTAlertControllerStyleActionSheet) {
            NSUInteger currentButtonIndex = [self.actionSheet addButtonWithTitle:action.title];

            if (action.style == PSTAlertActionStyleDestructive) {
                self.actionSheet.destructiveButtonIndex = currentButtonIndex;
            } else if (action.style == PSTAlertActionStyleCancel) {
                self.actionSheet.cancelButtonIndex = currentButtonIndex;
            }
        } else {
            NSUInteger currentButtonIndex = [self.alertView addButtonWithTitle:action.title];

            // UIAlertView doesn't support destructive buttons.
            if (action.style == PSTAlertActionStyleCancel) {
                self.alertView.cancelButtonIndex = currentButtonIndex;
            }
        }
    }
}

添加点击行为,也是根据 alertControllerAvailable 先判断是不是iOS8 以前的系统.如果不是,就调用 UIAlertView 或者 UIActionSheet的添加按钮的方式添加。

- (NSInteger)addButtonWithTitle:(nullable NSString *)title;    // returns index of button. 0 based.

需要执行的block则保存在actions 数组中。如果是iOS8之前的系统,则会在UIAlertView或者UIActionSheet的代理中执行,比如:

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
    [self viewWillDismissWithButtonIndex:buttonIndex];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    [self viewDidDismissWithButtonIndex:buttonIndex];
}

JCAlertController 一览
更加丰富的从iOS7开始支持的弹窗还有JCAlertController 详情见git地址。支持的样式更多,而且支持以一个队列的方式弹出。比如:

请看下面的伪代码:

[self presentWithFIFO:alert1];
[self presentWithFIFO:alert2];
[self presentWithFIFO:alert3];

结果:
alert1第一个显示, 被用户关闭后, alert2第二个显示, 被用户关闭后, alert3第三个显示
就像这样: alert1 >> alert2 >> alert3

JCAlertController的源码文件如下

|____.DS_Store
|____AlertView
| |____JCAlertView.h
| |____JCAlertView.m
|____ButtonItem
| |____JCAlertButtonItem.h
| |____JCAlertButtonItem.m
|____Category
| |____NSAttributedString+JCCalculateSize.h
| |____NSAttributedString+JCCalculateSize.m
| |____UIColor+JCHightlightedColor.h
| |____UIColor+JCHightlightedColor.m
| |____UIImage+JCColor2Image.h
| |____UIImage+JCColor2Image.m
| |____UIViewController+JCPresentQueue.h // present category
| |____UIViewController+JCPresentQueue.m
| |____UIWindow+JCBlur.h
| |____UIWindow+JCBlur.m
|____JCAlertController.h // import this
|____JCAlertController.m
|____Style
| |____JCAlertStyle.h
| |____JCAlertStyle.m
| |____JCAlertStyleAlertView.h
| |____JCAlertStyleAlertView.m
| |____JCAlertStyleBackground.h
| |____JCAlertStyleBackground.m
| |____JCAlertStyleButton.h
| |____JCAlertStyleButton.m
| |____JCAlertStyleButtonCancel.h
| |____JCAlertStyleButtonCancel.m
| |____JCAlertStyleButtonNormal.h
| |____JCAlertStyleButtonNormal.m
| |____JCAlertStyleButtonWarning.h
| |____JCAlertStyleButtonWarning.m
| |____JCAlertStyleContent.h
| |____JCAlertStyleContent.m
| |____JCAlertStyleSeparator.h
| |____JCAlertStyleSeparator.m
| |____JCAlertStyleTitle.h
| |____JCAlertStyleTitle.m

平时使用的时候,只要import JCAlertController即可!
JCAlertController 中定义了一些初始化和添加元素的方法。
同时定义了一个分类,是当有键盘事件的时候怎么处理。

@interface JCAlertController (keyboardHandle)

看看 JCAlertController的组成

@interface JCAlertController ()

@property (nonatomic, strong) UIImageView *blurView;
@property (nonatomic, strong) UIButton *coverView;
@property (nonatomic, strong) JCAlertView *alertView;

@end

一个有模糊样式的背景图片。一个可以接收点击的时间的遮挡按钮,然后就是看到的提示框 JCAlertView

然后定义了 转场动画。

///消失的动画
@interface JCDimissAnimation : NSObject 
@end
///弹出的动画
@interface JCPresentAnimation : NSObject 
@end

用来展示提示框变化的过渡动画!

- (void)viewDidLoad {
    [super viewDidLoad];
    ///设置背景模糊图片
    [self setupBlurView];
  ///添加点击按钮
    [self setupAlphaCoverView];
///根据整体Style设置alertView
    [self setupAlertView];
}

唯一不能满足的就是没有ActionSheet的样式

你可能感兴趣的:(iOS PSTAlertController 源码泛读)