iOS封装 AlertView提示框

////  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

//调用

#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:@"取消"];

}

你可能感兴趣的:(iOS封装 AlertView提示框)