私记-AlertController封装

39 篇文章 0 订阅
3 篇文章 0 订阅
  • AlertController.h文件

#import <Foundation/Foundation.h>

@interface AlertController : NSObject

/**
 alert

 @param title 标题
 @param message 内容
 @param actionTitles 按钮数组
 @param controller 显示控制器
 @param clickAtIndex 点击按钮的block
 */
+(void)showAlertViewWithTitle:(NSString *)title
                      message:(NSString *)message
                 ActionTitles:(NSArray *) actionTitles
                   controller:(id) controller
                      handler:(void(^)(NSInteger index)) clickAtIndex;

/**
 alert

 @param title 标题
 @param message 内容
 @param text 当前field的内容
 @param placeholder field的占位符
 @param actionTitles 按钮数组
 @param controller 显示控制器
 @param clickAtIndex 点击按钮的block
 */
+(void)showAlertFieldWithTitle:(NSString *)title
                       message:(NSString *)message
                     fieldText:(NSString *)text
              fieldplaceholder:(NSString *)placeholder
                  ActionTitles:(NSArray *) actionTitles
                    controller:(id) controller
                       handler:(void(^)(NSString *text, NSInteger index)) clickAtIndex;


/**
 alert

 @param title 标题
 @param message 内容
 @param actionTitles 按钮数组
 @param controller 显示控制器
 @param clickAtIndex 点击按钮的block
 */
+(void)showAlertSheetWithTitle:(NSString *)title
                       message:(NSString *)message
                  ActionTitles:(NSArray *) actionTitles
                    controller:(id) controller
                       handler:(void(^)(NSInteger index)) clickAtIndex;


/**
 alert

 @param title 标题
 @param message 内容
 @param alignment 内容显示样式(对齐样式)
 @param actionTitle 单个按钮标题(只适合设置单个按钮)
 @param controller 显示控制器
 */
+(void)showAlertViewWithTitle:(NSString *)title
                       message:(NSString *)message
              messageAlignment:(NSTextAlignment) alignment
                   ActionTitle:(NSString *) actionTitle
                    controller:(id) controller;
@end
  • AlertController.m文件
  • -

#import "AlertController.h"

@implementation AlertController



+(void)showAlertViewWithTitle:(NSString *)title
                      message:(NSString *)message
                 ActionTitles:(NSArray *) actionTitles
                   controller:(id) controller
                      handler:(void(^)(NSInteger index)) clickAtIndex
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

        for (NSInteger i = 0; i<actionTitles.count; i++) {
            UIAlertAction *action = [UIAlertAction actionWithTitle:actionTitles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (clickAtIndex) {
                    clickAtIndex(i);
                }
            }];
            [alertVc addAction:action];
        }

        [controller presentViewController:alertVc animated:YES completion:nil];
    });
}

+(void)showAlertFieldWithTitle:(NSString *)title
                       message:(NSString *)message
                     fieldText:(NSString *)text
              fieldplaceholder:(NSString *)placeholder
                  ActionTitles:(NSArray *) actionTitles
                    controller:(id) controller
                       handler:(void(^)(NSString *text, NSInteger index)) clickAtIndex
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

        [alertVc addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            textField.placeholder = placeholder;
            textField.text = text;
        }];

        for (NSInteger i = 0; i<actionTitles.count; i++) {
            UIAlertAction *action = [UIAlertAction actionWithTitle:actionTitles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (clickAtIndex) {
                    clickAtIndex(alertVc.textFields[0].text,i);
                }

            }];
            [alertVc addAction:action];
        }

        [controller presentViewController:alertVc animated:YES completion:nil];
    });
}

+(void)showAlertSheetWithTitle:(NSString *)title
                       message:(NSString *)message
                  ActionTitles:(NSArray *) actionTitles
                    controller:(id) controller
                       handler:(void(^)(NSInteger index)) clickAtIndex
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];

        for (NSInteger i = 0; i<actionTitles.count; i++) {
            UIAlertAction *action = [UIAlertAction actionWithTitle:actionTitles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (clickAtIndex) {
                    clickAtIndex(i);
                }

            }];
            [alertVc addAction:action];
        }
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        }];
        [alertVc addAction:cancel];

        [controller presentViewController:alertVc animated:YES completion:nil];
    });
}

+(void)showAlertViewWithTitle:(NSString *)title
                       message:(NSString *)message
              messageAlignment:(NSTextAlignment) alignment
                   ActionTitle:(NSString *) actionTitle
                    controller:(id) controller
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

        UIView *subView1 = alertVc.view.subviews[0];
        UIView *subView2 = subView1.subviews[0];
        UIView *subView3 = subView2.subviews[0];
        UIView *subView4 = subView3.subviews[0];
        UIView *subView5 = subView4.subviews[0];
        //取title和message:
        UILabel *title = subView5.subviews[0];
        UILabel *message = subView5.subviews[1];
        message.textAlignment = alignment;
        if (actionTitle) {
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:actionTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

            }];
            [alertVc addAction:cancel];
        }
        [controller presentViewController:alertVc animated:YES completion:nil];
    });
}


@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值