OC
+ (void)showAlertWithTitle:(nullable NSString *)title;
+ (void)showAlertWithTitle:(nullable NSString *)title message:(nullable NSString *)message;
+ (void)showAlertWithTitle:(nullable NSString *)title message:(nullable NSString *)message cancelButtonTitle:(nullable NSString *)cancelButtonTitle;
+ (void)showAlertWithTitle:(nullable NSString *)title message:(nullable NSString *)message cancelButtonTitle:(nullable NSString *)cancelButtonTitle actionHandler:(nullable void (^)(NSInteger buttonIndex))actionHandler;
+ (void)showAlertWithTitle:(nullable NSString *)title message:(nullable NSString *)message cancelButtonTitle:(nullable NSString *)cancelButtonTitle otherButtonTitles:(nullable NSArray *)otherButtonTitles actionHandler:(nullable void (^)(NSInteger buttonIndex))actionHandler;
+ (void)showAlertWithTitle:(nullable NSString *)title
{
[self showAlertWithTitle:title message:nil];
}
+ (void)showAlertWithTitle:(nullable NSString *)title message:(nullable NSString *)message
{
[self showAlertWithTitle:title message:message cancelButtonTitle:nil];
}
+ (void)showAlertWithTitle:(nullable NSString *)title message:(nullable NSString *)message cancelButtonTitle:(nullable NSString *)cancelButtonTitle
{
[self showAlertWithTitle:title message:message cancelButtonTitle:cancelButtonTitle actionHandler:nil];
}
+ (void)showAlertWithTitle:(nullable NSString *)title message:(nullable NSString *)message cancelButtonTitle:(nullable NSString *)cancelButtonTitle actionHandler:(nullable void (^)(NSInteger buttonIndex))actionHandler
{
[self showAlertWithTitle:title message:message cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil actionHandler:actionHandler];
}
+ (void)showAlertWithTitle:(nullable NSString *)title message:(nullable NSString *)message cancelButtonTitle:(nullable NSString *)cancelButtonTitle otherButtonTitles:(nullable NSArray *)otherButtonTitles actionHandler:(void (^)(NSInteger buttonIndex))actionHandler
{
cancelButtonTitle = cancelButtonTitle ? cancelButtonTitle : @"取消";
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
__weak typeof(alertController) weakController = alertController;
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSInteger index = [weakController.actions indexOfObject:action];
if (actionHandler) {
actionHandler(index);
}
}];
[alertController addAction:cancelAction];
for (NSString *title in otherButtonTitles) {
UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (actionHandler) {
NSInteger index = [weakController.actions indexOfObject:action];
actionHandler(index);
}
}];
[alertController addAction:action];
}
UIViewController *curVC = [(UIWindow *)[UIApplication sharedApplication].keyWindow visibleViewController];
[curVC presentViewController:alertController animated:YES completion:nil];
}
Swift
typealias action1Block = (() -> Void)?
typealias action2Block = (() -> Void)?
func showAlertViewWithOneContrl(_ alertInfo: (String?, String?, UIAlertController.Style),
action1info: (String, UIAlertAction.Style)?,
action1Block: action1Block) {
showAlertViewWith(alertInfo, action1info: action1info, action2info: nil, action1Block: {
if let ac1Block = action1Block {
ac1Block()
}
}, action2Block: nil)
}
func showAlertViewWith(_ alertInfo: (String?, String?, UIAlertController.Style),
action1info: (String, UIAlertAction.Style)?,
action2info: (String, UIAlertAction.Style)?,
action1Block: action1Block,
action2Block: action2Block) {
let alertViewController = UIAlertController(title: alertInfo.0, message: alertInfo.1, preferredStyle: alertInfo.2)
if let infoLeft = action1info {
let alertAction1 = UIAlertAction(title: infoLeft.0, style: infoLeft.1) { (action1) in
if let ac1Block = action1Block {
ac1Block()
}
}
alertViewController.addAction(alertAction1)
}
if let infoRight = action2info {
let alertAction2 = UIAlertAction(title: infoRight.0, style: infoRight.1) { (action2) in
if let ac2Block = action2Block {
ac2Block()
}
}
alertViewController.addAction(alertAction2)
}
UIViewController.top()?.present(alertViewController, animated: true, completion: nil)
}
网友评论