Skip to content

Commit

Permalink
初始提交
Browse files Browse the repository at this point in the history
  • Loading branch information
JXT committed Jun 22, 2018
1 parent f9590d5 commit 81a2dd0
Show file tree
Hide file tree
Showing 15 changed files with 1,238 additions and 0 deletions.
138 changes: 138 additions & 0 deletions JXTAlertController/JXTAlertController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
//
// JXTAlertController.h
// JXTAlertManager
//
// Created by JXT on 2016/12/22.
// Copyright © 2016年 JXT. All rights reserved.
//

#import <UIKit/UIKit.h>


NS_ASSUME_NONNULL_BEGIN

#pragma mark - I.JXTAlertController构造

@class JXTAlertController;
/**
JXTAlertController: alertAction配置链
@param title 标题
@return JXTAlertController对象
*/
typedef JXTAlertController * _Nonnull (^JXTAlertActionTitle)(NSString *title);

/**
JXTAlertController: alert按钮执行回调
@param buttonIndex 按钮index(根据添加action的顺序)
@param action UIAlertAction对象
@param alertSelf 本类对象
*/
typedef void (^JXTAlertActionBlock)(NSInteger buttonIndex, UIAlertAction *action, JXTAlertController *alertSelf);


/**
JXTAlertController 简介:
1.针对系统UIAlertController封装,支持iOS8及以上
2.关于iOS9之后的`preferredAction`属性用法:
`alertController.preferredAction = alertController.actions[0];`
效果为将已存在的某个action字体加粗,原cancel样式的加粗字体成为deafult样式,cancel样式的action仍然排列在最下
总体意义不大,且仅限于`UIAlertControllerStyleAlert`,actionSheet无效,功能略微鸡肋,不再单独封装
3.关于`addTextFieldWithConfigurationHandler:`方法:
该方法同样仅限于`UIAlertControllerStyleAlert`使用,使用场景较为局限,推荐直接调用,不再针对封装
4.关于自定义按钮字体或者颜色,可以利用kvc间接访问这些私有属性,但是不推荐
`[alertAction setValue:[UIColor grayColor] forKey:@"titleTextColor"]`
*/
NS_CLASS_AVAILABLE_IOS(8_0) @interface JXTAlertController : UIAlertController


/**
JXTAlertController: 禁用alert弹出动画,默认执行系统的默认弹出动画
*/
- (void)alertAnimateDisabled;

/**
JXTAlertController: alert弹出后,可配置的回调
*/
@property (nullable, nonatomic, copy) void (^alertDidShown)(void);

/**
JXTAlertController: alert关闭后,可配置的回调
*/
@property (nullable, nonatomic, copy) void (^alertDidDismiss)(void);

/**
JXTAlertController: 设置toast模式展示时间:如果alert未添加任何按钮,将会以toast样式展示,这里设置展示时间,默认1s
*/
@property (nonatomic, assign) NSTimeInterval toastStyleDuration; //deafult jxt_alertShowDurationDefault = 1s


/**
JXTAlertController: 链式构造alert视图按钮,添加一个alertAction按钮,默认样式,参数为标题
@return JXTAlertController对象
*/
- (JXTAlertActionTitle)addActionDefaultTitle;

/**
JXTAlertController: 链式构造alert视图按钮,添加一个alertAction按钮,取消样式,参数为标题(warning:一个alert该样式只能添加一次!!!)
@return JXTAlertController对象
*/
- (JXTAlertActionTitle)addActionCancelTitle;

/**
JXTAlertController: 链式构造alert视图按钮,添加一个alertAction按钮,警告样式,参数为标题
@return JXTAlertController对象
*/
- (JXTAlertActionTitle)addActionDestructiveTitle;

@end


#pragma mark - II.UIViewController扩展使用JXTAlertController

/**
JXTAlertController: alert构造块
@param alertMaker JXTAlertController配置对象
*/
typedef void(^JXTAlertAppearanceProcess)(JXTAlertController *alertMaker);

@interface UIViewController (JXTAlertController)

/**
JXTAlertController: show-alert(iOS8)
@param title title
@param message message
@param appearanceProcess alert配置过程
@param actionBlock alert点击响应回调
*/
- (void)jxt_showAlertWithTitle:(nullable NSString *)title
message:(nullable NSString *)message
appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess
actionsBlock:(nullable JXTAlertActionBlock)actionBlock NS_AVAILABLE_IOS(8_0);

/**
JXTAlertController: show-actionSheet(iOS8)
@param title title
@param message message
@param appearanceProcess actionSheet配置过程
@param actionBlock actionSheet点击响应回调
*/
- (void)jxt_showActionSheetWithTitle:(nullable NSString *)title
message:(nullable NSString *)message
appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess
actionsBlock:(nullable JXTAlertActionBlock)actionBlock NS_AVAILABLE_IOS(8_0);

@end

NS_ASSUME_NONNULL_END
216 changes: 216 additions & 0 deletions JXTAlertController/JXTAlertController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
//
// JXTAlertController.m
// JXTAlertManager
//
// Created by JXT on 2016/12/22.
// Copyright © 2016年 JXT. All rights reserved.
//

#import "JXTAlertController.h"

//toast默认展示时间
static NSTimeInterval const JXTAlertShowDurationDefault = 1.0f;


#pragma mark - I.AlertActionModel
@interface JXTAlertActionModel : NSObject
@property (nonatomic, copy) NSString * title;
@property (nonatomic, assign) UIAlertActionStyle style;
@end
@implementation JXTAlertActionModel
- (instancetype)init
{
if (self = [super init]) {
self.title = @"";
self.style = UIAlertActionStyleDefault;
}
return self;
}
@end



#pragma mark - II.JXTAlertController
/**
AlertActions配置
@param actionBlock JXTAlertActionBlock
*/
typedef void (^JXTAlertActionsConfig)(JXTAlertActionBlock actionBlock);


@interface JXTAlertController ()
//JXTAlertActionModel数组
@property (nonatomic, strong) NSMutableArray <JXTAlertActionModel *>* jxt_alertActionArray;
//是否操作动画
@property (nonatomic, assign) BOOL jxt_setAlertAnimated;
//action配置
- (JXTAlertActionsConfig)alertActionsConfig;
@end

@implementation JXTAlertController

- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
if (self.alertDidDismiss) {
self.alertDidDismiss();
}
}
- (void)dealloc
{
// NSLog(@"test-dealloc");
}

#pragma mark - Private
//action-title数组
- (NSMutableArray<JXTAlertActionModel *> *)jxt_alertActionArray
{
if (_jxt_alertActionArray == nil) {
_jxt_alertActionArray = [NSMutableArray array];
}
return _jxt_alertActionArray;
}
//action配置
- (JXTAlertActionsConfig)alertActionsConfig
{
return ^(JXTAlertActionBlock actionBlock) {
if (self.jxt_alertActionArray.count > 0)
{
//创建action
__weak typeof(self)weakSelf = self;
[self.jxt_alertActionArray enumerateObjectsUsingBlock:^(JXTAlertActionModel *actionModel, NSUInteger idx, BOOL * _Nonnull stop) {
UIAlertAction *alertAction = [UIAlertAction actionWithTitle:actionModel.title style:actionModel.style handler:^(UIAlertAction * _Nonnull action) {
__strong typeof(weakSelf)strongSelf = weakSelf;
if (actionBlock) {
actionBlock(idx, action, strongSelf);
}
}];
//可利用这个改变字体颜色,但是不推荐!!!
// [alertAction setValue:[UIColor grayColor] forKey:@"titleTextColor"];
//action作为self元素,其block实现如果引用本类指针,会造成循环引用
[self addAction:alertAction];
}];
}
else
{
NSTimeInterval duration = self.toastStyleDuration > 0 ? self.toastStyleDuration : JXTAlertShowDurationDefault;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:!(self.jxt_setAlertAnimated) completion:NULL];
});
}
};
}

#pragma mark - Public

- (instancetype)initAlertControllerWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle
{
if (!(title.length > 0) && (message.length > 0) && (preferredStyle == UIAlertControllerStyleAlert)) {
title = @"";
}
self = [[self class] alertControllerWithTitle:title message:message preferredStyle:preferredStyle];
if (!self) return nil;

self.jxt_setAlertAnimated = NO;
self.toastStyleDuration = JXTAlertShowDurationDefault;

return self;
}

- (void)alertAnimateDisabled
{
self.jxt_setAlertAnimated = YES;
}

- (JXTAlertActionTitle)addActionDefaultTitle
{
//该block返回值不是本类属性,只是局部变量,不会造成循环引用
return ^(NSString *title) {
JXTAlertActionModel *actionModel = [[JXTAlertActionModel alloc] init];
actionModel.title = title;
actionModel.style = UIAlertActionStyleDefault;
[self.jxt_alertActionArray addObject:actionModel];
return self;
};
}

- (JXTAlertActionTitle)addActionCancelTitle
{
return ^(NSString *title) {
JXTAlertActionModel *actionModel = [[JXTAlertActionModel alloc] init];
actionModel.title = title;
actionModel.style = UIAlertActionStyleCancel;
[self.jxt_alertActionArray addObject:actionModel];
return self;
};
}

- (JXTAlertActionTitle)addActionDestructiveTitle
{
return ^(NSString *title) {
JXTAlertActionModel *actionModel = [[JXTAlertActionModel alloc] init];
actionModel.title = title;
actionModel.style = UIAlertActionStyleDestructive;
[self.jxt_alertActionArray addObject:actionModel];
return self;
};
}

@end



#pragma mark - III.UIViewController扩展
@implementation UIViewController (JXTAlertController)

- (void)jxt_showAlertWithPreferredStyle:(UIAlertControllerStyle)preferredStyle title:(NSString *)title message:(NSString *)message appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess actionsBlock:(JXTAlertActionBlock)actionBlock
{
if (appearanceProcess)
{
JXTAlertController *alertMaker = [[JXTAlertController alloc] initAlertControllerWithTitle:title message:message preferredStyle:preferredStyle];
//防止nil
if (!alertMaker) {
return ;
}
//加工链
appearanceProcess(alertMaker);
//配置响应
alertMaker.alertActionsConfig(actionBlock);
// alertMaker.alertActionsConfig(^(NSInteger buttonIndex, UIAlertAction *action){
// if (actionBlock) {
// actionBlock(buttonIndex, action);
// }
// });

if (alertMaker.alertDidShown)
{
[self presentViewController:alertMaker animated:!(alertMaker.jxt_setAlertAnimated) completion:^{
alertMaker.alertDidShown();
}];
}
else
{
[self presentViewController:alertMaker animated:!(alertMaker.jxt_setAlertAnimated) completion:NULL];
}
}
}

- (void)jxt_showAlertWithTitle:(NSString *)title message:(NSString *)message appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess actionsBlock:(JXTAlertActionBlock)actionBlock
{
[self jxt_showAlertWithPreferredStyle:UIAlertControllerStyleAlert title:title message:message appearanceProcess:appearanceProcess actionsBlock:actionBlock];
}

- (void)jxt_showActionSheetWithTitle:(NSString *)title message:(NSString *)message appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess actionsBlock:(JXTAlertActionBlock)actionBlock
{
[self jxt_showAlertWithPreferredStyle:UIAlertControllerStyleActionSheet title:title message:message appearanceProcess:appearanceProcess actionsBlock:actionBlock];
}

@end

0 comments on commit 81a2dd0

Please sign in to comment.