forked from kgn/BBlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIAlertView+BBlock.m
46 lines (38 loc) · 1.39 KB
/
UIAlertView+BBlock.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
//
// UIAlertView+BBlock.m
// BBlock
//
// Created by Dominic Tham on 5/14/12.
// Updated by David Keegan on 6/4/12.
//
#import "UIAlertView+BBlock.h"
#import <objc/runtime.h>
static char UIAlertViewDelegateBBlockKey;
@interface UIAlertViewBBlockDelegate : NSObject
<UIAlertViewDelegate>
@property (strong, nonatomic) UIAlertViewBBlock block;
@end
@implementation UIAlertViewBBlockDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(self.block){
self.block(buttonIndex, alertView);
}
}
@end
@implementation UIAlertView(BBlock)
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelTitle
otherButtonTitle:(NSString *)otherButtonTitle completionBlock:(UIAlertViewBBlock)block{
if((self = [self initWithTitle:title message:message delegate:nil
cancelButtonTitle:cancelTitle otherButtonTitles:otherButtonTitle, nil])){
[self setCompletionBlock:block];
return self;
}
return nil;
}
- (void)setCompletionBlock:(UIAlertViewBBlock)block{
UIAlertViewBBlockDelegate *alertViewDelegate = [[UIAlertViewBBlockDelegate alloc] init];
objc_setAssociatedObject(self, &UIAlertViewDelegateBBlockKey, alertViewDelegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
alertViewDelegate.block = block;
self.delegate = alertViewDelegate;
}
@end