-
Notifications
You must be signed in to change notification settings - Fork 1
/
UIAlertView+Blocks.m
53 lines (38 loc) · 1.13 KB
/
UIAlertView+Blocks.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
//
// UIAlertView+Blocks.m
// GitLabViewer
//
// Created by Jon Staff on 2/13/14.
// Copyright (c) 2014 Indatus. All rights reserved.
//
#import "UIAlertView+Blocks.h"
#import <objc/runtime.h>
@interface INAlertWrapper : NSObject
@property (copy) void(^completionBlock)(UIAlertView *alertView, NSInteger buttonIndex);
@end
@implementation INAlertWrapper
#pragma mark - UIAlertViewDelegate
- (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 kINAlertWrapper;
@implementation UIAlertView (Blocks)
- (void)showWithCompletion:(void (^)(UIAlertView *, NSInteger))completion
{
INAlertWrapper *wrapper = [INAlertWrapper new];
wrapper.completionBlock = completion;
self.delegate = wrapper;
objc_setAssociatedObject(self, &kINAlertWrapper, wrapper, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self show];
}
@end