1.代码如下:
@interface AlertView : UIView
+ (AlertView *)shareAlertView;
/**带有单按钮的弹出视图 颜色可以不填*/
- (void)alertWithTitle:(NSString *)title message:(NSString *)message btnTitle:(NSString *)btnTitle btnTitleColor:(nullable UIColor *)btnTitleColor completion:(void(^)(void))completion;
/**带有双按钮的弹出视图 颜色可以不填*/
- (void)alertWIthTitle:(NSString *)title message:(NSString *)message leftBtnTitle:(NSString *)leftBtnTitle leftBtnTitleColor:(nullable UIColor *)leftBtnTitleColor leftBtnClicked:(void(^)(void))leftBtnClicked rightBtnTitle:(NSString *)rightBtnTitle rightBtnTitleColor:(nullable UIColor *)rightBtnTitleColor rightBtnClicked:(void(^)(void))rightBtnClicked;
@end
#import "AlertView.h"
@implementation AlertView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
+ (AlertView *)shareAlertView {
static AlertView *view = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
view = [[AlertView alloc]init];
});
return view;
}
- (void)alertWithTitle:(NSString *)title message:(NSString *)message btnTitle:(nonnull NSString *)btnTitle btnTitleColor:(nullable UIColor*)btnTitleColor completion:(void (^)(void))completion {
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:btnTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
completion();
}];
if (btnTitleColor) {
[action setValue:btnTitleColor forKey:@"_titleTextColor"];
}
[alertC addAction:action];
[[AlertView shareAlertView].viewController presentViewController:alertC animated:YES completion:nil];
}
- (void)alertWIthTitle:(NSString *)title message:(NSString *)message leftBtnTitle:(NSString *)leftBtnTitle leftBtnTitleColor:(nullable UIColor *)leftBtnTitleColor leftBtnClicked:(void (^)(void))leftBtnClicked rightBtnTitle:(NSString *)rightBtnTitle rightBtnTitleColor:(nullable UIColor *)rightBtnTitleColor rightBtnClicked:(void (^)(void))rightBtnClicked {
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:leftBtnTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
leftBtnClicked();
}];
if (leftBtnTitleColor) {
[action1 setValue:leftBtnTitleColor forKey:@"_titleTextColor"];
}
UIAlertAction *action2 = [UIAlertAction actionWithTitle:rightBtnTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
rightBtnClicked();
}];
if (rightBtnTitleColor) {
[action2 setValue:rightBtnTitleColor forKey:@"_titleTextColor"];
}
[alertC addAction:action1];
[alertC addAction:action2];
[[AlertView shareAlertView].viewController presentViewController:alertC animated:YES completion:nil];
}
#pragma 获取当前的视图控制器
- (UIViewController *)viewController {
UIViewController *vc = [[UIApplication sharedApplication].keyWindow rootViewController];
return vc;
}
@end
2.使用:
[[AlertView shareAlertView]alertWithTitle:@"提示" message:@"单按钮提示框" btnTitle:@"确定" btnTitleColor:[UIColor orangeColor] completion:^{
NSLog(@"按钮被点击了");
}];
[[AlertView shareAlertView]alertWIthTitle:@"提示" message:@"双按钮提示" leftBtnTitle:@"取消" leftBtnTitleColor:[UIColor redColor] leftBtnClicked:^{
NSLog(@"左按钮点击了");
} rightBtnTitle:@"确定" rightBtnTitleColor:nil rightBtnClicked:^{
NSLog(@"右按钮点击了");
}];