链式alertViewController

快速创建alertview

1. 自定义创建一个弹框需要自己配置 AlertAction
  ZFAlertManager*m= [ZFAlertManager makerAlertTitle:@"提示" message:@"测试信息" block:^(ZFAlertManager * _Nonnull maker) {
        ZFAlertAction *action = [ZFAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        action.textColor = [UIColor redColor];
        ZFAlertAction *action1 = [ZFAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        action1.textColor = [UIColor orangeColor];
        maker.addCustomAction(action).addCustomAction(action1);
    }];
    [m showAlertInController:self animated:YES];
2.快速创建alert
 [[[ZFAlertManager makerAlertTitle:@"提示" message:@"测试测试测试测试测试信息" block:^(ZFAlertManager * _Nonnull maker) {
                maker.addDefaultTitle(@"测试").addDefaultTitle(@"测试2");
        }] onDidClickAction:^(ZFAlertAction * _Nonnull action) {
            NSLog(@"%@",action.title);
        }]showAlertInController:self animated:YES];
3. 快速创建一个actionsheet
 [[[ZFAlertManager makerSheetTitle:@"提示" message:@"测试测试测试测试测试信息" block:^(ZFAlertManager * _Nonnull maker) {
                maker.addDefaultTitle(@"测试").addDefaultTitle(@"测试2");
        }] onDidClickAction:^(ZFAlertAction * _Nonnull action) {
            NSLog(@"%@",action.title);
        }]showAlertInController:self animated:YES];
4.快速创建一个有textfield 的alert
 [[[ZFAlertManager makerAlertTitle:@"提示" message:@"测试测试测试测试测试信息" block:^(ZFAlertManager * _Nonnull maker) {
        maker.addDefaultTitle(@"测试").addDefaultTitle(@"测试2").addTextField(^(UITextField *textfied){
            textfied.placeholder = @"账号";
            
        });
        }] onDidClickAction:^(ZFAlertAction * _Nonnull action) {
            NSLog(@"%@",action.title);
        }]showAlertInController:self animated:YES];

.h文件

///手动添加一个action (需要自己配置action)
-(ZFAlertManager* (^)(ZFAlertAction *action))addCustomAction;

///添加一个defaultAction title
-(ZFAlertManager*(^)(NSString *title))addDefaultTitle;
///添加一个cancelAction title
-(ZFAlertManager*(^)(NSString *title))addCencalTitle;
///添加一个DestructiveAction title
-(ZFAlertManager*(^)(NSString *title))addDestructiveTitle;
///添加一个textField
-(ZFAlertManager*(^)(TextFiedBlock block))addTextField;

///点击action回调
-(ZFAlertManager*)onDidClickAction:(ActionBlock)block;

///alert 视图
-(ZFAlertManager*)showAlertInController:(UIViewController *)control animated:(BOOL)animated;

.m文件

-(ZFAlertManager* (^)(ZFAlertAction *action))addCustomAction{
    __weak typeof(self) weakSelf = self;
    return ^(ZFAlertAction *action){
        [weakSelf addAction:action];
        return self;
    };
}
///添加一个defaultAction title
-(ZFAlertManager*(^)(NSString *title))addDefaultTitle{
    return [self addCustomAction:UIAlertActionStyleDefault];
}
///添加一个cancelAction title
-(ZFAlertManager*(^)(NSString *title))addCencalTitle{
    return [self addCustomAction:UIAlertActionStyleCancel];
}
///添加一个DestructiveAction title
-(ZFAlertManager*(^)(NSString *title))addDestructiveTitle{
    return [self addCustomAction:UIAlertActionStyleDestructive];
}
///添加一个textField
-(ZFAlertManager*(^)(TextFiedBlock block))addTextField{
    NSAssert(self.preferredStyle != UIAlertControllerStyleActionSheet, @"actionSheet 不能添加textFied");
    __weak typeof(self) weakSelf = self;
    return ^(TextFiedBlock block){
        __strong typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            block(textField);
        }];
        return strongSelf;
    };
}
///公共样式
-(ZFAlertManager*(^)(NSString *title))addCustomAction:(UIAlertActionStyle)style{
    __weak typeof(self) weakSelf = self;
    return ^(NSString *title){
        __strong typeof(weakSelf)strongSelf = weakSelf;
        ZFAlertAction *zfaction = [ZFAlertAction actionWithTitle:title style:style block:^(ZFAlertAction * _Nonnull action) {
            if(weakSelf.block){
                weakSelf.block(action);
            }
        }];
        return strongSelf.addCustomAction(zfaction);
    };
}

///回调
-(ZFAlertManager*)onDidClickAction:(ActionBlock)block{
    self.block = block;
    return  self;
}
-(ZFAlertManager*)showAlertInController:(UIViewController *)control animated:(BOOL)animated{
    NSAssert(self, @"需要初始化项目initWithTitle: message: preferredStyle:");
    if (control == nil) {
        [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:self animated:YES completion:nil];
    }else{
        [control presentViewController:self animated:animated completion:^{
            
        }];
    }
    return self;
}
-(void)dealloc{
    NSLog(@"alert销毁了");
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • { 11、核心动画 需要签协议,但是系统帮签好 一、CABasicAnimation 1、创建基础动画对象 CAB...
    CYC666阅读 1,598评论 2 4
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,611评论 28 53
  • 人工智能是什么?什么是人工智能?人工智能是未来发展的必然趋势吗?以后人工智能技术真的能达到电影里机器人的智能水平吗...
    ZLLZ阅读 3,939评论 0 5
  • 首先介绍下自己的背景: 我11年左右入市到现在,也差不多有4年时间,看过一些关于股票投资的书籍,对于巴菲特等股神的...
    瞎投资阅读 5,792评论 3 8
  • ![Flask](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAW...
    极客学院Wiki阅读 7,407评论 0 3