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 <UIKit/UIKit.h>

@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


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,545评论 25 709
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    wgl0419阅读 11,485评论 1 9
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,684评论 2 45
  • 很少在这上写东西……我都是在QQ空间上写日志,只有自己知道。 写到这上面,我的水平达不到让别人欣赏的地步。。写的都...
    行走在路上的大粪阅读 689评论 0 1
  • 七个月了,女人未踏出这个二楼的房间一步,望向窗外,到处是水,店铺几乎都关了门,小船载着行色匆匆的人在水中穿梭,阳光...
    布衣荆钗阅读 1,157评论 0 0