ZCAlertController.h
//
// ZCAlertController.h
// qiyuan
//
// Created by zhangchao on 2017/10/26.
// Copyright © 2017年 jiuqi. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ZCAlertController : UIAlertController
/**
初始化弹框
@param title 弹框的标题
@param message 弹框的展示的信息
@param style 0或者1;代表弹框的类型;UIAlertControllerStyleActionSheet = 0,UIAlertControllerStyleAlert = 1
@param titleArray 弹框中出现的按钮标题的数组
@param alertAction block回调事件
*/
+ (instancetype)initZCAlertControllerWithTitle:(NSString *)title message:(NSString *)message style:(NSString *)style titleArray:(NSMutableArray *)titleArray alertAction:(void (^)(NSInteger index))alertAction;
/**
展示弹框
*/
- (void)showZCAlert;
@end
ZCAlertController.m
//
// ZCAlertController.m
// qiyuan
//
// Created by zhangchao on 2017/10/26.
// Copyright © 2017年 jiuqi. All rights reserved.
//
#import "ZCAlertController.h"
@interface ZCAlertController ()
@end
@implementation ZCAlertController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
+ (instancetype)initZCAlertControllerWithTitle:(NSString *)title message:(NSString *)message style:(NSString *)style titleArray:(NSMutableArray *)titleArray alertAction:(void (^)(NSInteger))alertAction
{
if ([style isEqualToString:@"1"]) {
ZCAlertController *alert = [ZCAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
for (NSInteger i = 0; i < titleArray.count; i++) {
UIAlertAction *confirm = [UIAlertAction actionWithTitle:[titleArray objectAtIndex:i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (alertAction) {
alertAction(i);
}
}];
[alert addAction:confirm];
}
return alert;
}else{
ZCAlertController *alert = [ZCAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
for (NSInteger i = 0; i < titleArray.count; i++) {
UIAlertAction *confirm = [UIAlertAction actionWithTitle:[titleArray objectAtIndex:i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (alertAction) {
alertAction(i);
}
}];
[alert addAction:confirm];
}
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:cancelAction];
return alert;
}
}
- (void)showZCAlert
{
[[self getCurrentVC] presentViewController:self animated:YES completion:nil];
}
//获取当前的VC
-(UIViewController *)getCurrentVC
{
UIViewController *result = nil;
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
if (window.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow * tempWindow in windows)
{
if (tempWindow.windowLevel == UIWindowLevelNormal)
{
window = tempWindow;
break;
}
}
}
UIView *frontView = [[window subviews] objectAtIndex:0];
id nextResponder = [frontView nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]])
result = nextResponder;
else
result = window.rootViewController;
return result;
}
@end