链式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销毁了");
}

你可能感兴趣的:(链式alertViewController)