JLChen
2021-08-02 38f4fb064df09f344fc3237409c76a9fba2a8a9e
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
//  DTAlertView.m
//  DTFoundation
//
//  Created by Oliver Drobnik on 11/22/12.
//  Copyright (c) 2012 Cocoanetics. All rights reserved.
//
 
#import "DTAlertView.h"
 
@interface DTAlertView() <UIAlertViewDelegate>
 
@end
 
@implementation DTAlertView
{
    NSMutableDictionary *_actionsPerIndex;
 
    DTAlertViewBlock _cancelBlock;
}
 
- (void)dealloc
{
    [super setDelegate:nil];
    self.alertViewDelegate = nil;
}
 
// designated initializer
- (id)init
{
    self = [super init];
    if (self)
    {
        _actionsPerIndex = [[NSMutableDictionary alloc] init];
        [super setDelegate:self];
    }
    return self;
}
 
- (id)initWithTitle:(NSString *)title message:(NSString *)message
{
    return [self initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
}
 
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
{
    self = [self init];
    if (self)
    {
        self.title = title;
        self.message = message;
        
        if (otherButtonTitles != nil) {
            [self addButtonWithTitle:otherButtonTitles];
            va_list args;
            va_start(args, otherButtonTitles);
            NSString *title = nil;
            while( (title = va_arg(args, NSString *)) ) {
                [self addButtonWithTitle:title];
            }
            va_end(args);
        }
        if (cancelButtonTitle) {
            [self addCancelButtonWithTitle:cancelButtonTitle block:nil];
        }
        
        self.alertViewDelegate = delegate;
    }
    return self;
}
 
- (NSInteger)addButtonWithTitle:(NSString *)title block:(DTAlertViewBlock)block
{
    NSInteger retIndex = [self addButtonWithTitle:title];
 
    if (block)
    {
        NSNumber *key = [NSNumber numberWithInteger:retIndex];
        [_actionsPerIndex setObject:[block copy] forKey:key];
    }
 
    return retIndex;
}
 
- (NSInteger)addCancelButtonWithTitle:(NSString *)title block:(DTAlertViewBlock)block
{
    NSInteger retIndex = [self addButtonWithTitle:title block:block];
    [self setCancelButtonIndex:retIndex];
 
    return retIndex;
}
 
- (void)setCancelBlock:(DTAlertViewBlock)block
{
    _cancelBlock = block;
}
 
# pragma mark - UIAlertViewDelegate
 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSNumber *key = [NSNumber numberWithInteger:buttonIndex];
    
    DTAlertViewBlock block = [_actionsPerIndex objectForKey:key];
    if (block)
    {
        block();
    }
 
    if ([self.alertViewDelegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)])
    {
        [self.alertViewDelegate alertView:self clickedButtonAtIndex:buttonIndex];
    }
}
 
- (void)alertViewCancel:(UIAlertView *)alertView
{
    if (_cancelBlock)
    {
        _cancelBlock();
    }
 
    if ([self.alertViewDelegate respondsToSelector:@selector(alertViewCancel:)])
    {
        [self.alertViewDelegate alertViewCancel:self];
    }
}
 
- (void)willPresentAlertView:(UIAlertView *)alertView
{
    if ([self.alertViewDelegate respondsToSelector:@selector(willPresentAlertView:)])
    {
        [self.alertViewDelegate willPresentAlertView:self];
    }
}
 
- (void)didPresentAlertView:(UIAlertView *)alertView
{
    if ([self.alertViewDelegate respondsToSelector:@selector(didPresentAlertView:)])
    {
        [self.alertViewDelegate didPresentAlertView:self];
    }
}
 
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if ([self.alertViewDelegate respondsToSelector:@selector(alertView:willDismissWithButtonIndex:)])
    {
        [self.alertViewDelegate alertView:self willDismissWithButtonIndex:buttonIndex];
    }
}
 
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if ([self.alertViewDelegate respondsToSelector:@selector(alertView:didDismissWithButtonIndex:)])
    {
        [self.alertViewDelegate alertView:self didDismissWithButtonIndex:buttonIndex];
    }
}
 
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    if ([self.alertViewDelegate respondsToSelector:@selector(alertViewShouldEnableFirstOtherButton:)])
    {
        return [self.alertViewDelegate alertViewShouldEnableFirstOtherButton:self];
    }
 
    return YES;
}
 
 
#pragma mark - Properties
 
 
- (void)setDelegate:(id <UIAlertViewDelegate>)delegate
{
    if (delegate)
    {
        NSLog(@"Calling setDelegate is not supported! Use setAlertViewDelegate instead");
    }
}
 
@end