iOS UIAlertController简单封装,用Block
2016-11-17 本文已影响191人
在这蓝色天空下
在iOS8中,UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一种模块化替换的方式来代替这两货的功能和作用。是使用对话框(alert)还是使用上拉菜单(actionsheet),就取决于在创建控制器时,您是如何设置首选样式的。demo:https://github.com/tianzhilan0/LCAlertController.git
1、UIAlertControllerStyleAlert 用法
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:actionName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
//点击此按钮的时候,在这里逻辑处理
}];
UIAlertAction *left=[UIAlertAction actionWithTitle:leftName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
//点击此按钮的时候,在这里逻辑处理
}];
[alertController addAction:action];
[self presentViewController:alertController animated:YES completion:nil];
2、UIAlertControllerStyleActionSheet 用法
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:one style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//点击此按钮的时候,在这里逻辑处理
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:two style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//点击此按钮的时候,在这里逻辑处理
}];
[alertController addAction:cancelAction];
[alertController addAction:action1];
[alertController addAction:action2];
[self presentViewController:alertController animated:YES completion:nil];
3、给alert添加输入框
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = placeholder;//提醒
textField.secureTextEntry = YES;//密文输入
}];
UIAlertAction *right=[UIAlertAction actionWithTitle:rightName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
UITextField *textField = alertController.textFields.firstObject;
}];
UIAlertAction *left=[UIAlertAction actionWithTitle:leftName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
UITextField *textField = alertController.textFields.firstObject;
}];
[alertController addAction:right];
[alertController addAction:left];
[viewController presentViewController:alertController animated:YES completion:nil];
简单封装 .h
//
// LCAlertHelper.h
// Custom
//
// Created by LC on 16/11/17.
// Copyright © 2016年 xiu&yun. All rights reserved.
//https://github.com/tianzhilan0/LCAlertController.git
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface LCAlertHelper : NSObject
/**
* 一个Action
*/
+ (void)alertWithTitle:(NSString *)title
message:(NSString *)message
actionName:(NSString *)actionName
viewController:(UIViewController *)viewController
action:(void(^)())oneAction;
/**
* 两个Action
*/
+ (void)alertWithTitle:(NSString *)title
message:(NSString *)message
rightName:(NSString *)rightName
leftName:(NSString *)leftName
viewController:(UIViewController *)viewController
rightAction:(void (^)())rightAction
leftAction:(void (^)())leftAction;
/**
* sheet,有提醒
*/
+ (void)sheetAlertWithTitle:(NSString *)title
message:(NSString *)message
one:(NSString *)one
two:(NSString *)two
viewController:(UIViewController *)viewController
oneAction:(void(^)())oneAction
twoAction:(void(^)())twoAction;
/**
* sheet,无提醒
*/
+ (void)sheetAlertWithOne:(NSString *)one
two:(NSString *)two
viewController:(UIViewController *)viewController
oneAction:(void(^)())oneAction
twoAction:(void(^)())twoAction;
/**
* 有输入框
*/
+ (void)addTextFieldWithTitle:(NSString *)title
message:(NSString *)message
rightName:(NSString *)rightName
leftName:(NSString *)leftName
placeholder:(NSString *)placeholder
viewController:(UIViewController *)viewController
rightAction:(void (^)(NSString *text))rightAction
leftAction:(void (^)(NSString *text))leftAction;
/**
* 有输入框 密文输入
*/
+ (void)addSecureTextFieldWithTitle:(NSString *)title
message:(NSString *)message
rightName:(NSString *)rightName
leftName:(NSString *)leftName
placeholder:(NSString *)placeholder
viewController:(UIViewController *)viewController
rightAction:(void (^)(NSString *text))rightAction
leftAction:(void (^)(NSString *text))leftAction;
@end
.m
//
// LCAlertHelper.m
// Custom
//
// Created by LC on 16/11/17.
// Copyright © 2016年 xiu&yun. All rights reserved.
//
//https://github.com/tianzhilan0/LCAlertController.git
#import "LCAlertHelper.h"
@implementation LCAlertHelper
/**
* 一个Action
*/
+ (void)alertWithTitle:(NSString *)title
message:(NSString *)message
actionName:(NSString *)actionName
viewController:(UIViewController *)viewController
action:(void(^)())oneAction
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *right=[UIAlertAction actionWithTitle:actionName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
if (oneAction) {
oneAction();
}
}];
[alertController addAction:right];
[viewController presentViewController:alertController animated:YES completion:nil];
}
/**
* 两个Action
*/
+ (void)alertWithTitle:(NSString *)title
message:(NSString *)message
rightName:(NSString *)rightName
leftName:(NSString *)leftName
viewController:(UIViewController *)viewController
rightAction:(void (^)())rightAction
leftAction:(void (^)())leftAction
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *right=[UIAlertAction actionWithTitle:rightName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
if (rightAction) {
rightAction();
}
}];
UIAlertAction *left=[UIAlertAction actionWithTitle:leftName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
if (leftAction) {
leftAction();
}
}];
[alertController addAction:right];
[alertController addAction:left];
[viewController presentViewController:alertController animated:YES completion:nil];
}
/**
* sheet,有提醒
*/
+ (void)sheetAlertWithTitle:(NSString *)title
message:(NSString *)message
one:(NSString *)one
two:(NSString *)two
viewController:(UIViewController *)viewController
oneAction:(void(^)())oneAction
twoAction:(void(^)())twoAction
{
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:one style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
if (oneAction) {
oneAction();
}
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:two style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
if (twoAction) {
twoAction();
}
}];
[alertController addAction:cancelAction];
[alertController addAction:action1];
[alertController addAction:action2];
[viewController presentViewController:alertController animated:YES completion:nil];
}
/**
* sheet,无提醒
*/
+ (void)sheetAlertWithOne:(NSString *)one
two:(NSString *)two
viewController:(UIViewController *)viewController
oneAction:(void(^)())oneAction
twoAction:(void(^)())twoAction
{
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:one style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
if (oneAction) {
oneAction();
}
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:two style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
if (twoAction) {
twoAction();
}
}];
[alertController addAction:cancelAction];
[alertController addAction:action1];
[alertController addAction:action2];
[viewController presentViewController:alertController animated:YES completion:nil];
}
/**
* 有输入框
*/
+ (void)addTextFieldWithTitle:(NSString *)title
message:(NSString *)message
rightName:(NSString *)rightName
leftName:(NSString *)leftName
placeholder:(NSString *)placeholder
viewController:(UIViewController *)viewController
rightAction:(void (^)(NSString *))rightAction
leftAction:(void (^)(NSString *))leftAction
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = placeholder;
}];
UIAlertAction *right=[UIAlertAction actionWithTitle:rightName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
UITextField *textField = alertController.textFields.firstObject;
if (rightAction) {
rightAction(textField.text);
}
}];
UIAlertAction *left=[UIAlertAction actionWithTitle:leftName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
UITextField *textField = alertController.textFields.firstObject;
if (leftAction) {
leftAction(textField.text);
}
}];
[alertController addAction:right];
[alertController addAction:left];
[viewController presentViewController:alertController animated:YES completion:nil];
}
/**
* 有输入框 密文输入
*/
+ (void)addSecureTextFieldWithTitle:(NSString *)title
message:(NSString *)message
rightName:(NSString *)rightName
leftName:(NSString *)leftName
placeholder:(NSString *)placeholder
viewController:(UIViewController *)viewController
rightAction:(void (^)(NSString *))rightAction
leftAction:(void (^)(NSString *))leftAction
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = placeholder;
textField.secureTextEntry = YES;
}];
UIAlertAction *right=[UIAlertAction actionWithTitle:rightName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
UITextField *textField = alertController.textFields.firstObject;
if (rightAction) {
rightAction(textField.text);
}
}];
UIAlertAction *left=[UIAlertAction actionWithTitle:leftName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
UITextField *textField = alertController.textFields.firstObject;
if (leftAction) {
leftAction(textField.text);
}
}];
[alertController addAction:right];
[alertController addAction:left];
[viewController presentViewController:alertController animated:YES completion:nil];
}
@end