1、自定义弹
创建一个继承与NSObject的类CGXAlertController
2、在.h中写
/*
title: 标题
message: 提示语
buttonTitles: 按钮选项数组
completionBlock: 按钮点击返回block
vc: 返回控制器
*/
#pragma mark - 样式选择 返回的有title
+ (void)showAlertViewStyleWithTitle:(NSString*)title message:(NSString*)message buttonTitles:(NSArray*)buttonTitles titleBlock:(void(^)(NSString *titleStr ,NSString *messageStr ,NSString *btntitleStr))titleBlock showViewController:(UIViewController*)vc preferredStyle:(UIAlertControllerStyle)style;
3、在.m中写
static void (^G_completionBlock)(int buttonindex);
#pragma mark - 样式选择 返回的有title
+ (void)showAlertViewStyleWithTitle:(NSString*)title message:(NSString*)message buttonTitles:(NSArray*)buttonTitles titleBlock:(void(^)(NSString *titleStr ,NSString *messageStr ,NSString *btntitleStr))titleBlock showViewController:(UIViewController*)vc preferredStyle:(UIAlertControllerStyle)style{
[CGXAlertController showAlertViewControllerWithTitle:title message:message buttonTitles:buttonTitles showViewController:vc completionBlock:^(NSString *Btntitle) {
if (titleBlock) {
titleBlock(title,message,Btntitle);
}
} cancleBlock:^{
} preferredStyle:style];
}
#pragma mark - 自定义样式
+ (void)showAlertViewControllerWithTitle:(NSString*)title message:(NSString*)message buttonTitles:(NSArray*)buttonTitles showViewController:(UIViewController*)vc completionBlock:(void(^)(NSString *Btntitle))completionBlock cancleBlock:(void(^)(void))cancelBlock preferredStyle:(UIAlertControllerStyle)style{
#ifdef NSFoundationVersionNumber_iOS_8_0
UIAlertController* alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:style];
for (NSString* titleStr in buttonTitles) {
UIAlertAction* action;
if ([titleStr isEqualToString:@"取消"] || [titleStr isEqualToString:@"cancel"]) {
action = [UIAlertAction actionWithTitle:titleStr style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
if (cancelBlock) {
cancelBlock();
}
}];
}else {
action = [UIAlertAction actionWithTitle:titleStr style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (completionBlock) {
completionBlock(titleStr);
}
}];
}
[alert addAction:action];
}
[vc presentViewController:alert animated:YES completion:nil];
#else
G_completionBlock = [completionblock copy];
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:strtitle message:strmessage delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
for (NSString *key in buttontitles) {
[alert addButtonWithTitle:key];
}
[alert show];
#endif
}
#pragma clang diagnostic ignored "-Wdeprecated"
+(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (G_completionBlock) {
G_completionBlock((int)buttonIndex);
}
}