UIAlertController分类
此分类对UIAlertController进行封装, 可以直接在外部一行代码调用。
功能包含UIAlertController和actionSheet样式的弹框,点击相关按钮收到相应回调。
使用
// 显示一个按钮的alert
[UIAlertController showAlertControllerTitle:@"提示" msg:@"提示内容XXXX" btnTitle:@"知道了" callBack:^{
// 点击的回调
}];
// 显示两个按钮的alert
[UIAlertController showAlertControllerTitle:@"提示" msg:@"提示内容XXXXX" leftTitle:@"取消" rightTitle:@"确定" leftCallBack:^{
// TODO: 点击取消的回调
} rightCallBack:^{
// TODO: 点击确定的回调
}];
// 显示一个按钮的actionSheet
[UIAlertController showAlertControllerActionSheetWithTitle:@"请选择" actionArr:@[@"选择1", @"选择2", @"选择3"] callBack:^(NSString *selectedActionStr) {
NSLog(@"选择的内容-----%@", selectedActionStr);
}];
源码
UIAlertController+Category.h
+ (void)showAlertControllerTitle:(NSString *)title
msg:(NSString *)msg
leftTitle:(NSString *)leftTitle
rightTitle:(NSString *)rightTitle
leftCallBack:(void(^)(void))leftCallBack
rightCallBack:(void(^)(void))rightCallBack;
+ (void)showAlertControllerTitle:(NSString *)title
msg:(NSString *)msg
btnTitle:(NSString *)btnTitle
callBack:(void(^)(void))callBack;
// actionSheet
+ (void)showAlertControllerActionSheetWithTitle:(NSString *)title
actionArr:(nonnull NSArray <NSString *>*)actionArr
callBack:(void(^)(NSString *selectedActionStr))callBack;
+ (void)dissmissAlertVC;
UIAlertController+Category.m
+ (void)showAlertControllerTitle:(NSString *)title
msg:(NSString *)msg
leftTitle:(NSString *)leftTitle
rightTitle:(NSString *)rightTitle
leftCallBack:(void(^)(void))leftCallBack
rightCallBack:(void(^)(void))rightCallBack {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:(UIAlertControllerStyleAlert)];
UIAlertAction *leftAction = [UIAlertAction actionWithTitle:leftTitle style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
if (leftCallBack) {
leftCallBack();
}
}];
UIAlertAction *rightAction = [UIAlertAction actionWithTitle:rightTitle style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
if (rightCallBack) {
rightCallBack();
}
}];
[alertController addAction:leftAction];
[alertController addAction:rightAction];
[[self getCurrentVC] presentViewController:alertController animated:YES completion:nil];
}
+ (void)showAlertControllerTitle:(NSString *)title
msg:(NSString *)msg
btnTitle:(NSString *)btnTitle
callBack:(void(^)(void))callBack {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:(UIAlertControllerStyleAlert)];
UIAlertAction *leftAction = [UIAlertAction actionWithTitle:btnTitle style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
if (callBack) {
callBack();
}
}];
[alertController addAction:leftAction];
[[self getCurrentVC] presentViewController:alertController animated:YES completion:nil];
}
+ (void)showAlertControllerActionSheetWithTitle:(NSString *)title
actionArr:(nonnull NSArray <NSString *>*)actionArr
callBack:(void(^)(NSString *selectedActionStr))callBack {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
for (NSString *actionStr in actionArr) {
UIAlertAction *alertAction = [UIAlertAction actionWithTitle:actionStr style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
if (callBack) {
callBack(actionStr);
}
}];
[alertVC addAction:alertAction];
}
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
[alertVC addAction:cancelAction];
[[self getCurrentVC] presentViewController:alertVC animated:YES completion:nil];
}
/// 关闭alertVC
+ (void)dissmissAlertVC {
UIViewController *presentedAlertVC = [self getCurrentVC].presentedViewController;
if (!presentedAlertVC) return;
if ([presentedAlertVC isKindOfClass:[UIAlertController class]]) {
[presentedAlertVC dismissViewControllerAnimated:NO completion:nil];
}
}
/*
获取当前屏幕显示的控制器
*/
+ (UIViewController *)getCurrentVC
{
UIViewController *rootViewController =[[[[UIApplication sharedApplication] delegate] window] rootViewController];
return [self getVisibleViewControllerFrom:rootViewController];
}
+ (UIViewController *)getVisibleViewControllerFrom:(UIViewController *)vc {
if ([vc isKindOfClass:[UINavigationController class]]) {
return [self getVisibleViewControllerFrom:[((UINavigationController *)vc) visibleViewController]];
} else if ([vc isKindOfClass:[UITabBarController class]]) {
return [self getVisibleViewControllerFrom:[((UITabBarController *)vc) selectedViewController]];
} else {
if (vc.presentedViewController) {
return [self getVisibleViewControllerFrom:vc.presentedViewController];
} else {
return vc;
}
}
}