iOS 一句话实现系统Alert(SLAlertController)

说在前头

github:https://github.com/wslCoding/SLAlertController

1.使用

 [SLAlertController sl_alertWithTitle:@"登录" message:@"输入账号和密码"].addTextField(^(UITextField *textField, UIAlertController *alert) {
      textField.placeholder = @"账号";
  }).addTextField(^(UITextField *textField, UIAlertController *alert) {
      textField.placeholder = @"密码";
  }).addDefaultButton(@"登录", ^(UIAlertAction *action, UIAlertController *alert) {

   }).addDestructiveButton(@"注册", ^(UIAlertAction *action, UIAlertController *alert) {

   }).addCancelButton(@"取消", ^(UIAlertAction *action, UIAlertController *alert) {

   }).show(###需要显示的控制器###);

2.实现代码

SLAlertController.h

//
//  SLAlertController.h
//  SLAlertController
//
//  Created by 王胜利 on 2018/7/5.
//  Copyright © 2018年 wsl. All rights reserved.
//

#import 

@class SLAlertController;

typedef SLAlertController *(^AlertButton)(NSString *title,void(^handle)(UIAlertAction *action,UIAlertController *alert));
typedef SLAlertController *(^AlertTextField)(void(^handle)(UITextField *textField,UIAlertController *alert));
typedef void(^ShowAlert)(UIViewController *controller);


@interface SLAlertController : UIAlertController

/// 显示系统Alert(iOS 8 +)
+ (SLAlertController *)sl_alertWithTitle:(NSString *)title message:(NSString *)message NS_AVAILABLE_IOS(8_0);
/// 显示系统ActionSheet(iOS8 +)
+ (SLAlertController *)sl_actionSheetWithTitle:(NSString *)title message:(NSString *)message NS_AVAILABLE_IOS(8_0);

/// 添加Cancel按钮
@property (nonatomic,copy,readonly) AlertButton addCancelButton;
/// 添加Destructive按钮
@property (nonatomic,copy,readonly) AlertButton addDestructiveButton;
/// 添加Default按钮
@property (nonatomic,copy,readonly) AlertButton addDefaultButton;
/// 添加TextField输入框
@property (nonatomic,copy,readonly) AlertTextField addTextField;
/// 显示Alert
@property (nonatomic,copy,readonly) ShowAlert show;

@end

SLAlertController.m

//
//  SLAlertController.m
//  SLAlertController
//
//  Created by 王胜利 on 2018/7/5.
//  Copyright © 2018年 wsl. All rights reserved.
//

#import "SLAlertController.h"

@implementation SLAlertController


+ (SLAlertController *)sl_alertWithTitle:(NSString *)title message:(NSString *)message{
    return  [SLAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
}

+ (SLAlertController *)sl_actionSheetWithTitle:(NSString *)title message:(NSString *)message{
    return  [SLAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
}

- (AlertButton)addCancelButton{
    return [self addButtonWithStyle:UIAlertActionStyleCancel];
}
- (AlertButton)addDestructiveButton{
    return [self addButtonWithStyle:UIAlertActionStyleDestructive];
}
- (AlertButton)addDefaultButton{
    return [self addButtonWithStyle:UIAlertActionStyleDefault];
}
- (AlertButton)addButtonWithStyle:(UIAlertActionStyle)style{
    return ^id(NSString *title,void(^handler)(UIAlertAction *,UIAlertController *)){
        __weak typeof(self)weakSelf = self;
        [self addAction:[UIAlertAction actionWithTitle:title style:style handler:^(UIAlertAction * _Nonnull action) {
            __strong typeof(weakSelf)strongSelf = weakSelf;
            if (handler) handler(action,strongSelf);
        }]];
        return self;
    };
}

- (AlertTextField)addTextField{
    return ^id(void(^handler)(UITextField *,UIAlertController *)){
        __weak typeof(self)weakSelf = self;
        [self addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            __strong typeof(weakSelf)strongSelf = weakSelf;
          if (handler) handler(textField,strongSelf);
        }];
        return self;
    };
}

- (ShowAlert)show{
    return ^void(UIViewController *controller){
        dispatch_async(dispatch_get_main_queue(), ^{
            [controller presentViewController:self animated:YES completion:nil];
        });
    };
}

@end


你可能感兴趣的:(iOS 一句话实现系统Alert(SLAlertController))