-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathUIAlertViewX.m
43 lines (28 loc) · 974 Bytes
/
UIAlertViewX.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
#import "UIAlertViewX.h"
#import <objc/runtime.h>
@interface XAlertViewWrapper : NSObject
@property (copy) UIAlertViewCompletionBlock completionBlock;
@end
@implementation XAlertViewWrapper
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (self.completionBlock)
self.completionBlock(alertView, buttonIndex);
}
- (void)alertViewCancel:(UIAlertView *)alertView
{
if (self.completionBlock)
self.completionBlock(alertView, alertView.cancelButtonIndex);
}
@end
static const char kXAlertViewWrapper;
@implementation UIAlertView (X)
- (void)showWithCompletion:(UIAlertViewCompletionBlock)completionBlock
{
XAlertViewWrapper *alertViewWrapper = XAlertViewWrapper.new;
alertViewWrapper.completionBlock = completionBlock;
self.delegate = alertViewWrapper;
objc_setAssociatedObject(self, &kXAlertViewWrapper, alertViewWrapper, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self show];
}
@end