-
-
Notifications
You must be signed in to change notification settings - Fork 808
Expand file tree
/
Copy pathSimpleAlert.m
More file actions
69 lines (54 loc) · 2.01 KB
/
SimpleAlert.m
File metadata and controls
69 lines (54 loc) · 2.01 KB
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
#import "SimpleAlert.h"
@interface SimpleAlert ()
@property (nonatomic, copy) NSString *titleText;
@property (nonatomic, copy) NSString *messageText;
@property (nonatomic, strong) NSMutableArray<SimpleAlertButton *> *buttons;
@end
@implementation SimpleAlertButton
- (instancetype)initWithTitle:(NSString *)title {
if (self = [super init]) {
self.title = title;
self.isCancel = NO;
}
return self;
}
- (SimpleAlertButton *)handler:(SimpleAlertHandler)handler {
self.handler = handler;
return self;
}
- (SimpleAlertButton *)cancelStyle {
self.isCancel = YES;
return self;
}
@end
@implementation SimpleAlert
+ (void)makeAlert:(void (^)(SimpleAlert *make))block showFrom:(UIViewController *)viewController {
SimpleAlert *alert = [[SimpleAlert alloc] init];
alert.buttons = [NSMutableArray array];
block(alert);
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alert.titleText
message:alert.messageText
preferredStyle:UIAlertControllerStyleAlert];
for (SimpleAlertButton *btn in alert.buttons) {
UIAlertActionStyle style = btn.isCancel ? UIAlertActionStyleCancel : UIAlertActionStyleDefault;
UIAlertAction *action = [UIAlertAction actionWithTitle:btn.title style:style handler:^(UIAlertAction * _Nonnull action) {
if (btn.handler) {
btn.handler(@[btn.title]);
}
}];
[alertController addAction:action];
}
[viewController presentViewController:alertController animated:YES completion:nil];
}
- (void)title:(NSString *)title {
self.titleText = title;
}
- (void)message:(NSString *)message {
self.messageText = message;
}
- (SimpleAlertButton *)button:(NSString *)title {
SimpleAlertButton *btn = [[SimpleAlertButton alloc] initWithTitle:title];
[self.buttons addObject:btn];
return btn;
}
@end