#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
#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];
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