-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZKAlertManager.m
91 lines (70 loc) · 3.19 KB
/
ZKAlertManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// ZKAlertManager.m
// SSAI
//
// Created by zhangkeqin on 2023/12/21.
// Copyright © 2021 ZK. All rights reserved.
//
#import "ZKAlertManager.h"
@interface ZKAlertManager()
@property (nonatomic, copy) void(^cancelButtonHandler)(void);
@property (nonatomic, copy) void(^doneButtonHandler)(void);
@end
@implementation ZKAlertManager
+ (ZKAlertManager*)shared{
static dispatch_once_t once;
static ZKAlertManager *shared;
dispatch_once(&once, ^ {
shared = [[ZKAlertManager alloc] init];
});
return shared;
}
-(void)showWithTitle:(id)title message:(id)message cancelText:(NSString *)cancelText cancelColor:(id)cancelColor doneText:(NSString *)doneText doneColor:(id)doneColor controller:(UIViewController *)controller cancelButtonHandler:(void (^)(void))cancelButtonHandler doneButtonHandler:(void (^)(void))doneButtonHandler{
self.cancelButtonHandler = cancelButtonHandler;
self.doneButtonHandler = doneButtonHandler;
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:(UIAlertControllerStyleAlert)];
//标题
if ([title isKindOfClass:[NSString class]]) {
NSString *titleString = (NSString *)title;
[alertController setValue:titleString forKey:@"title"];
} else if ([title isKindOfClass:[NSMutableAttributedString class]]) {
NSMutableAttributedString *attributedTitle = (NSMutableAttributedString *)title;
[alertController setValue:attributedTitle forKey:@"attributedTitle"];
}
//内容
if ([message isKindOfClass:[NSString class]]) {
NSString *messageString = (NSString *)message;
[alertController setValue:messageString forKey:@"message"];
} else if ([message isKindOfClass:[NSMutableAttributedString class]]) {
NSMutableAttributedString *attributedMessage = (NSMutableAttributedString *)message;
[alertController setValue:attributedMessage forKey:@"attributedMessage"];
}
//取消按钮
if(cancelText.length){
UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:cancelText style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if(self.cancelButtonHandler){
self.cancelButtonHandler();
}
}];
if([cancelColor isKindOfClass:[UIColor class]]){
[cancleAction setValue:cancelColor forKey:@"titleTextColor"];
}else{
[cancleAction setValue:[UIColor grayColor] forKey:@"titleTextColor"];
}
[alertController addAction:cancleAction];
}
//确定按钮
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:doneText style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
if(self.doneButtonHandler){
self.doneButtonHandler();
}
}];
if([doneColor isKindOfClass:[UIColor class]]){
[sureAction setValue:doneColor forKey:@"titleTextColor"];
}else{
[sureAction setValue:[UIColor blackColor] forKey:@"titleTextColor"];
}
[alertController addAction:sureAction];
[controller presentViewController:alertController animated:YES completion:nil];
}
@end