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);
}
}