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
//
//  DTActionSheet.m
//  DTFoundation
//
//  Created by Oliver Drobnik on 08.06.12.
//  Copyright (c) 2012 Cocoanetics. All rights reserved.
//
 
#import "DTActionSheet.h"
#import "DTWeakSupport.h"
 
@interface DTActionSheet () <UIActionSheetDelegate>
 
@end
 
@implementation DTActionSheet
{
    NSMutableDictionary *_actionsPerIndex;
}
 
// designated initializer
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        _actionsPerIndex = [[NSMutableDictionary alloc] init];
        [super setDelegate:self];
 
    }
    return self;
}
 
- (instancetype)initWithTitle:(NSString *)title
{
    return [self initWithTitle:title delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
}
 
- (instancetype)initWithTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
{
    self = [self init];
    if (self)
    {
        self.title = title;
 
        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 (destructiveButtonTitle) {
            [self addDestructiveButtonWithTitle:destructiveButtonTitle block:nil];
        }
        if (cancelButtonTitle) {
            [self addCancelButtonWithTitle:cancelButtonTitle block:nil];
        }
 
        self.actionSheetDelegate = delegate;
    }
 
    return self;
}
 
- (void)dealloc
{
    [super setDelegate:nil];
    self.actionSheetDelegate = nil;
}
 
- (NSInteger)addButtonWithTitle:(NSString *)title block:(DTActionSheetBlock)block
{
    NSInteger retIndex = [self addButtonWithTitle:title];
 
    if (block)
    {
        NSNumber *key = [NSNumber numberWithInteger:retIndex];
        [_actionsPerIndex setObject:[block copy] forKey:key];
    }
 
    return retIndex;
}
 
- (NSInteger)addDestructiveButtonWithTitle:(NSString *)title block:(DTActionSheetBlock)block
{
    NSInteger retIndex = [self addButtonWithTitle:title block:block];
    [self setDestructiveButtonIndex:retIndex];
 
    return retIndex;
}
 
- (NSInteger)addCancelButtonWithTitle:(NSString *)title
{
    return [self addCancelButtonWithTitle:title block:nil];
}
 
- (NSInteger)addCancelButtonWithTitle:(NSString *)title block:(DTActionSheetBlock)block
{
    NSInteger retIndex = [self addButtonWithTitle:title block:block];
    [self setCancelButtonIndex:retIndex];
 
    return retIndex;
}
 
#pragma mark - UIActionSheetDelegate (forwarded)
 
- (void)actionSheetCancel:(UIActionSheet *)actionSheet
{
    if ([self.actionSheetDelegate respondsToSelector:@selector(actionSheetCancel:)])
    {
        [self.actionSheetDelegate actionSheetCancel:actionSheet];
    }
}
 
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    if ([self.actionSheetDelegate respondsToSelector:@selector(willPresentActionSheet:)])
    {
        [self.actionSheetDelegate willPresentActionSheet:actionSheet];
    }
}
 
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet
{
    if ([self.actionSheetDelegate respondsToSelector:@selector(didPresentActionSheet:)])
    {
        [self.actionSheetDelegate didPresentActionSheet:actionSheet];
    }
}
 
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if ([self.actionSheetDelegate respondsToSelector:@selector(actionSheet:willDismissWithButtonIndex:)])
    {
        [self.actionSheetDelegate actionSheet:actionSheet willDismissWithButtonIndex:buttonIndex];
    }
}
 
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if ([self.actionSheetDelegate respondsToSelector:@selector(actionSheet:didDismissWithButtonIndex:)])
    {
        [self.actionSheetDelegate actionSheet:actionSheet didDismissWithButtonIndex:buttonIndex];
    }
}
 
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSNumber *key = [NSNumber numberWithInteger:buttonIndex];
 
    DTActionSheetBlock block = [_actionsPerIndex objectForKey:key];
 
    if (block)
    {
        block();
    }
 
    if ([self.actionSheetDelegate respondsToSelector:@selector(actionSheet:clickedButtonAtIndex:)])
    {
        [self.actionSheetDelegate actionSheet:actionSheet clickedButtonAtIndex:buttonIndex];
    }
}
 
#pragma mark - Properties
 
- (void)setDelegate:(id <UIActionSheetDelegate>)delegate
{
    if (delegate)
    {
        NSLog(@"Calling setDelegate is not supported! Use setActionSheetDelegate instead");
    }
}
 
@end