UIAlertController+Extention
Posted liuyongfa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIAlertController+Extention相关的知识,希望对你有一定的参考价值。
调用:
[UIAlertController showWithTitle:@"提示" buttonTitle:@"确定" block:^{ NSLog(@"确定"); }]; [UIAlertController showWithTitle:@"提示" message:nil rootViewController:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] actionBlocks:^{ NSLog(@"取消"); }, ^{ NSLog(@"确定"); }, nil];
UIAlertController+LYFExtention.h
#import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN typedef void(^AlertBlock)(void); @interface UIAlertController (LYFExtention) /** 显示UIAlertController,只有一个按钮 @param title title @param buttonTitle 按钮文字 @param block 按钮的回调 */ + (void)showWithTitle:(NSString *)title buttonTitle:(NSString *)buttonTitle block:(AlertBlock)block; /** 显示UIAlertController @param title title @param message message @param viewController 在哪个UIViewcontroller上显示,可以为nil @param cancelString 取消按钮文字,为nil则没有取消按钮 @param otherTitles 其他按钮文字 @param block 响应按钮的block,如果cancelString不为nil,则第一个为cancel按钮的,之后的block依次对应otherTitles的元素 */ + (void)showWithTitle:(NSString *)title message: (nullable NSString *)message rootViewController:(nullable UIViewController *)viewController cancelButtonTitle:(nullable NSString *)cancelString otherButtonTitles:(NSArray <NSString *>*)otherTitles actionBlocks:(AlertBlock)block, ...NS_REQUIRES_NIL_TERMINATION; @end NS_ASSUME_NONNULL_END
UIAlertController+LYFExtention.m
#import "UIAlertController+LYFExtention.h" @implementation UIAlertController (LYFExtention) + (void)showWithTitle:(NSString *)title buttonTitle:(NSString *)buttonTitle block:(AlertBlock)block { [UIAlertController showWithTitle:title ?: @"" message:@"" rootViewController:nil cancelButtonTitle:nil otherButtonTitles:@[buttonTitle ?: @"确定"] actionBlocks:^{ block(); }, nil]; } + (void)showWithTitle:(NSString *)title message: (nullable NSString *)message rootViewController:(nullable UIViewController *)viewController cancelButtonTitle:(nullable NSString *)cancelString otherButtonTitles:(NSArray <NSString *>*)otherTitles actionBlocks:(AlertBlock)block, ... { UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message ?: @"" preferredStyle:UIAlertControllerStyleAlert]; int i = 0; if (cancelString) { [alert addAction:[UIAlertAction actionWithTitle:cancelString style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { block(); }]]; } else { if (otherTitles.count > 0) { [alert addAction:[UIAlertAction actionWithTitle:otherTitles[0] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { block(); }]]; i = 1; } } if (otherTitles.count > i) { va_list arg_list; va_start(arg_list, block); AlertBlock tempBlock; while ((tempBlock = va_arg(arg_list, AlertBlock))) { if (i >= otherTitles.count) { break; } [alert addAction:[UIAlertAction actionWithTitle:otherTitles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { tempBlock(); }]]; i++; } va_end(arg_list); for (; i < otherTitles.count; i++) { [alert addAction:[UIAlertAction actionWithTitle:otherTitles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]]; } } if (!viewController) { viewController = [alert visibleViewController:nil]; if (!viewController) { return; } } [viewController presentViewController:alert animated:true completion:nil]; } - (UIViewController *)visibleViewController:(UIViewController *)rootViewController { UIViewController *root; if (rootViewController) { root = rootViewController; } else { UIViewController *controller = UIApplication.sharedApplication.delegate.window.rootViewController; if (!controller) { return nil; } root = controller; } UIViewController *presented = root.presentedViewController; if (!presented) { return root; } if ([presented isKindOfClass:[UINavigationController class]]) { return [self visibleViewController:((UINavigationController *)presented).topViewController]; } if ([presented isKindOfClass:[UITabBarController class]]) { return [self visibleViewController:((UITabBarController *)presented).selectedViewController]; } return presented; } @end
GitHub地址:
https://github.com/liuyongfa/UIAlertController-Extention.git
以上是关于UIAlertController+Extention的主要内容,如果未能解决你的问题,请参考以下文章
swift UIAlertController使用 UIAlertController的宽度 为270
从 AppDelegate 到 PresentedViewController 的警报:“尝试在...上呈现 UIAlertController 已经在呈现 UIAlertController”