-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleton.m
247 lines (214 loc) · 8.57 KB
/
Singleton.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//
// Singleton.m
// TabBarTest
//
// Created by Shebin Koshy on 11/18/16.
// Copyright © 2016 Shebin Koshy. All rights reserved.
//
#import "Singleton.h"
@interface Singleton ()<UIAlertViewDelegate,UIActionSheetDelegate>
@property(nonatomic,strong) NSMutableArray *arrayAlertActionHandlers, *arrayActionSheetActionHandlers;
@end
@implementation Singleton
+(instancetype)sharedInstance
{
static Singleton *singletonObj;
static dispatch_once_t token;
_dispatch_once(&token, ^{
singletonObj = [[Singleton alloc]init];
singletonObj.arrayAlertActionHandlers = [[NSMutableArray alloc]init];
singletonObj.arrayActionSheetActionHandlers = [[NSMutableArray alloc]init];
});
return singletonObj;
}
#pragma mark - Alert View
-(void)addAlertActionHandlerToArray:(id)alertActionHandler
{
if (alertActionHandler)
{
[_arrayAlertActionHandlers addObject:alertActionHandler];
}
else
{
[_arrayAlertActionHandlers addObject:[NSNull null]];
}
}
-(void)showAlertWithTitle:(NSString *)title message:(NSString *)message arrayOfOtherButtonTitles:(NSArray*)arrayButtonTitles cancelButtonTitle:(NSString*)cancelButtonTitle presentInViewController:(UIViewController*)viewController actionHandler:(void (^)(NSString * buttonTitle, NSInteger buttonIndex))actionHandler
{
if (arrayButtonTitles.count == 0 && !cancelButtonTitle)
{
/**
no buttons
*/
NSLog(@"ERROR: No Buttons");
return;
}
if ((([title isKindOfClass:[NSString class]] && title.length > 0) || ([message isKindOfClass:[NSString class]] && message.length > 0)) == NO)
{
/**
no title or message
*/
NSLog(@"ERROR: No title or message");
return;
}
if ([UIAlertController class])
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
for (NSString *buttonTitles in arrayButtonTitles)
{
UIAlertAction *action = [UIAlertAction actionWithTitle:buttonTitles style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self alertButtonActionsWithButtonIndex:[arrayButtonTitles indexOfObject:action.title]+1 withButtonTitle:action.title actionHandler:actionHandler];
}];
[alert addAction:action];
}
if (cancelButtonTitle)
{
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[self alertButtonActionsWithButtonIndex:0 withButtonTitle:action.title actionHandler:actionHandler];
}];
[alert addAction:actionCancel];
}
[viewController presentViewController:alert animated:YES completion:^{
[self addAlertActionHandlerToArray:actionHandler];
}];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
for (NSString *stringButtonTitle in arrayButtonTitles)
{
[alert addButtonWithTitle:stringButtonTitle];
}
if (cancelButtonTitle)
{
alert.cancelButtonIndex = [alert addButtonWithTitle:cancelButtonTitle];
}
[alert show];
[self addAlertActionHandlerToArray:actionHandler];
}
}
-(void)alertButtonActionsWithButtonIndex:(NSInteger)buttonIndex withButtonTitle:(NSString*)buttonTitle actionHandler:(id)actionHandler
{
[_arrayAlertActionHandlers removeLastObject];
if (actionHandler && [actionHandler isKindOfClass:[NSNull class]] == NO)
{
void (^blockName)(NSString * buttonTitle1, NSInteger buttonIndex1) = actionHandler;
{
blockName(buttonTitle,buttonIndex);
};
}
}
#pragma mark - UIAlertView delegate (iOS 7.0)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSInteger buttonIndexToPass = buttonIndex;
if (alertView.cancelButtonIndex == buttonIndex && alertView.cancelButtonIndex != 0)
{
/**
cancel button
*/
buttonIndexToPass = 0;
}
if (alertView.cancelButtonIndex != buttonIndex && alertView.cancelButtonIndex != 0)
{
/**
other buttons
*/
buttonIndexToPass++;
}
[self alertButtonActionsWithButtonIndex:buttonIndexToPass withButtonTitle:[alertView buttonTitleAtIndex:buttonIndex] actionHandler:[_arrayAlertActionHandlers lastObject]];
}
#pragma mark - Action sheet
-(void)addActionSheetActionHandlerToArray:(id)actionSheetActionHandler
{
if (actionSheetActionHandler)
{
[_arrayActionSheetActionHandlers addObject:actionSheetActionHandler];
}
else
{
[_arrayActionSheetActionHandlers addObject:[NSNull null]];
}
}
-(void)showActionSheetWithTitle:(NSString *)title arrayOfOtherButtonTitles:(NSArray*)arrayButtonTitles cancelButtonTitle:(NSString*)cancelButtonTitle presentInViewController:(UIViewController*)viewController actionHandler:(void (^)(NSString * buttonTitle, NSInteger buttonIndex))actionHandler;
{
if (arrayButtonTitles.count == 0 && !cancelButtonTitle)
{
/**
no buttons
*/
NSLog(@"ERROR: No Buttons");
return;
}
if ([UIAlertController class])
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];
for (NSString *buttonTitles in arrayButtonTitles)
{
UIAlertAction *action = [UIAlertAction actionWithTitle:buttonTitles style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self actionButtonActionsWithButtonIndex:[arrayButtonTitles indexOfObject:action.title]+1 withButtonTitle:action.title actionHandler:actionHandler];
}];
[alertController addAction:action];
}
if (cancelButtonTitle)
{
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[self actionButtonActionsWithButtonIndex:0 withButtonTitle:action.title actionHandler:actionHandler];
}];
[alertController addAction:actionCancel];
}
[viewController presentViewController:alertController animated:YES completion:^{
[self addActionSheetActionHandlerToArray:actionHandler];
}];
}
else
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
for (NSString *title in arrayButtonTitles)
{
[actionSheet addButtonWithTitle:title];
}
if (cancelButtonTitle)
{
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:cancelButtonTitle];
}
[actionSheet showInView:viewController.view];
[self addActionSheetActionHandlerToArray:actionHandler];
}
}
-(void)actionButtonActionsWithButtonIndex:(NSInteger)buttonIndex withButtonTitle:(NSString*)buttonTitle actionHandler:(id)actionHandler
{
[_arrayActionSheetActionHandlers removeLastObject];
if (actionHandler && [actionHandler isKindOfClass:[NSNull class]] == NO)
{
void (^blockName)(NSString * buttonTitle1, NSInteger buttonIndex1) = actionHandler;
{
blockName(buttonTitle,buttonIndex);
};
}
}
#pragma mark - UIActionSheet delegate (iOS 7.0)
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSInteger buttonIndexToPass = buttonIndex;
if (actionSheet.cancelButtonIndex == buttonIndex && actionSheet.cancelButtonIndex != 0)
{
/**
cancel button
*/
buttonIndexToPass = 0;
}
if (actionSheet.cancelButtonIndex != buttonIndex && actionSheet.cancelButtonIndex != 0)
{
/**
other buttons
*/
buttonIndexToPass++;
}
[self actionButtonActionsWithButtonIndex:buttonIndexToPass withButtonTitle:[actionSheet buttonTitleAtIndex:buttonIndex] actionHandler:[_arrayActionSheetActionHandlers lastObject]];
}
@end