////? AlertViewTool.h//? AlertActiionDemo////? Created by Max on 16/8/30.//? Copyright ? 2016年 maxzhang. All rights reserved.//#import#importtypedef void (^ActionBlockAtIndex)(NSInteger index);
@interface AlertViewTool : NSObject
@property (nonatomic, copy) ActionBlockAtIndex actionBlockAtIndex;
//UIAlertControllerStyleAlert邑闺,多個按鈕(中間)
+ (id)AlertAlertWithTitle:(NSString*)title Message:(NSString *)message otherItemArrays:(NSArray *)array viewController:(UIViewController *)controller handler:(ActionBlockAtIndex)block;
//用法
//? ? UIAlertControllerStyleAlert
//[AlertViewTool AlertAlertWithTitle:@"標題" Message:@"這是一條消息" otherItemArrays:@[@"按鈕1", @"按鈕2"] viewController:self handler:^(NSInteger index) {
//
//? ? if (index == 0) {
//? ? ? ? NSLog(@"點擊了按鈕1");
//? ? }
//? ? else if (index == 1) {
//? ? ? ? NSLog(@"點擊了按鈕2");
//? ? }
//
//}];
//UIAlertControllerStyleActionSheet 多個按鈕(底部) isShowCancel==是否展示取消按鈕芽唇,yes表示展示
+ (id)AlertSheetToolWithTitle:(NSString*)title Message:(NSString *)message otherItemArrays:(NSArray *)array viewController:(UIViewController *)controller handler:(ActionBlockAtIndex)block isShowCancel:(BOOL)isShow CancelTitle:(NSString*)cancetitle;
//用法
//? UIAlertControllerStyleActionSheet
//? ? [AlertViewTool AlertSheetToolWithTitle:@"標題" Message:@"這是一條消息" otherItemArrays:@[@"按鈕1", @"按鈕2", @"按鈕3"] viewController:self handler:^(NSInteger index) {
//? ? ? ? if (index == 0) {
//? ? ? ? ? ? ? ? NSLog(@"點擊了按鈕1");
//? ? ? ? ? ? }
//? ? ? ? ? ? else if (index == 1) {
//? ? ? ? ? ? ? ? NSLog(@"點擊了按鈕2");
//? ? ? ? ? ? }
//? ? ? ? ? ? else if (index == 2) {
//? ? ? ? ? ? ? ? NSLog(@"點擊了按鈕3");
//? ? ? ? ? ? }
//? ? ? ? ? ? else if (index == 2) {
//? ? ? ? ? ? ? ? NSLog(@"點擊了取消");
//? ? ? ? ? ? }
//
//? ? } isShowCancel:YES CancelTitle:@"取消"];
@end
//
//? AlertViewTool.m
//? AlertActiionDemo
//
//? Created by Max on 16/8/30.
//? Copyright ? 2016年 maxzhang. All rights reserved.
//
#import "AlertViewTool.h"
@implementation AlertViewTool
#pragma mark ==================AlertView=========================
//在中間提示的很多個按鈕
+ (id)AlertAlertWithTitle:(NSString*)title Message:(NSString *)message otherItemArrays:(NSArray *)array viewController:(UIViewController *)controller handler:(ActionBlockAtIndex)block{
return [[self alloc] initWithTitleCenter:title Message:message otherItemArrays:array viewController:controller handler:block];
}
//在中間提示的很多個按鈕
- (instancetype)initWithTitleCenter:(NSString *)title Message:(NSString*)message otherItemArrays:(NSArray *)array viewController:(UIViewController *)controller handler:(id)sender{
if ([self init]) {
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
self.actionBlockAtIndex = sender;
if (![array isKindOfClass:[NSNull class]] && array != nil && array.count) {
for (int i = 0; i < array.count; i++) {
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:array[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (self.actionBlockAtIndex) {
self.actionBlockAtIndex(i);
}
[alertC dismissViewControllerAnimated:YES completion:nil];
}];
[alertC addAction:otherAction];
}
}
[controller presentViewController:alertC animated:YES completion:nil];
}
return self;
}
#pragma mark ==================AlertSheetTool=========================
+ (id)AlertSheetToolWithTitle:(NSString*)title Message:(NSString *)message otherItemArrays:(NSArray *)array viewController:(UIViewController *)controller handler:(ActionBlockAtIndex)block isShowCancel:(BOOL)isShow CancelTitle:(NSString*)cancetitle
{
return [[self alloc] initWithCancelTitle:title Message:message otherItemArrays:array viewController:controller handler:block isShowCancel:isShow CancelTitle:cancetitle];
}
- (instancetype)initWithCancelTitle:(NSString *)title Message:(NSString*)message otherItemArrays:(NSArray *)array viewController:(UIViewController *)controller handler:(id)sender isShowCancel:(BOOL)isShowCancel CancelTitle:(NSString *)Cancetitle
{
if ([self init]) {
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
self.actionBlockAtIndex = sender;
if (isShowCancel == YES) {
UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:Cancetitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
if (self.actionBlockAtIndex) {
self.actionBlockAtIndex(array.count);
}
[alertC dismissViewControllerAnimated:YES completion:nil];
}];
[alertC addAction:cancelAction];
}
if (![array isKindOfClass:[NSNull class]] && array != nil && array.count) {
for (int i = 0; i < array.count; i++) {
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:array[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (self.actionBlockAtIndex) {
self.actionBlockAtIndex(i);
}
[alertC dismissViewControllerAnimated:YES completion:nil];
}];
[alertC addAction:otherAction];
}
}
[controller presentViewController:alertC animated:YES completion:nil];
}
return self;
}
@end
//調(diào)用
#import "AlertViewTool.h"
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event
{
//? ? UIAlertControllerStyleAlert
[AlertViewTool AlertAlertWithTitle:@"標題" Message:@"這是一條消息" otherItemArrays:@[@"按鈕1", @"按鈕2"] viewController:self handler:^(NSInteger index) {
if (index == 0) {
NSLog(@"點擊了按鈕1");
}
else if (index == 1) {
NSLog(@"點擊了按鈕2");
}
}];
//? UIAlertControllerStyleActionSheet
//? ? [AlertViewTool AlertSheetToolWithTitle:@"標題" Message:@"這是一條消息" otherItemArrays:@[@"按鈕1", @"按鈕2", @"按鈕3"] viewController:self handler:^(NSInteger index) {
//? ? ? ? if (index == 0) {
//? ? ? ? ? ? ? ? NSLog(@"點擊了按鈕1");
//? ? ? ? ? ? }
//? ? ? ? ? ? else if (index == 1) {
//? ? ? ? ? ? ? ? NSLog(@"點擊了按鈕2");
//? ? ? ? ? ? }
//? ? ? ? ? ? else if (index == 2) {
//? ? ? ? ? ? ? ? NSLog(@"點擊了按鈕3");
//? ? ? ? ? ? }
//? ? ? ? ? ? else if (index == 2) {
//? ? ? ? ? ? ? ? NSLog(@"點擊了取消");
//? ? ? ? ? ? }
//
//? ? } isShowCancel:YES CancelTitle:@"取消"];
}