-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleton.m
217 lines (186 loc) · 7.94 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
//
// 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 *arrayAlertDelegates, *arrayActionSheetDelegates;
@end
@implementation Singleton
+(instancetype)sharedInstance
{
static Singleton *singletonObj;
static dispatch_once_t token;
_dispatch_once(&token, ^{
singletonObj = [[Singleton alloc]init];
singletonObj.arrayAlertDelegates = [[NSMutableArray alloc]init];
singletonObj.arrayActionSheetDelegates = [[NSMutableArray alloc]init];
});
return singletonObj;
}
#pragma mark - Alert View
-(void)addAlertDelegatesToArray:(id)alertDelegate
{
if (alertDelegate)
{
[_arrayAlertDelegates addObject:alertDelegate];
}
else
{
[_arrayAlertDelegates addObject:[NSNull null]];
}
}
-(void)showAlertWithTag:(NSInteger)tag title:(NSString *)title message:(NSString *)message arrayOfOtherButtonTitles:(NSArray*)arrayButtonTitles cancelButtonTitle:(NSString*)cancelButtonTitle presentInViewController:(UIViewController*)viewController delegate:(id)alertDelegate
{
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 alertButtonActionsWithTag:tag buttonIndex:[arrayButtonTitles indexOfObject:action.title]+1 withButtonTitle:action.title alertDelegate:alertDelegate];
}];
[alert addAction:action];
}
if (cancelButtonTitle)
{
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[self alertButtonActionsWithTag:tag buttonIndex:0 withButtonTitle:action.title alertDelegate:alertDelegate];
}];
[alert addAction:actionCancel];
}
[viewController presentViewController:alert animated:YES completion:^{
[self addAlertDelegatesToArray:alertDelegate];
}];
}
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.tag = tag;
[alert show];
[self addAlertDelegatesToArray:alertDelegate];
}
}
-(void)alertButtonActionsWithTag:(NSInteger)tag buttonIndex:(NSInteger)buttonIndex withButtonTitle:(NSString*)buttonTitle alertDelegate:(id<AlertDelegate>)alertDelegate
{
[_arrayAlertDelegates removeLastObject];
if (alertDelegate && [alertDelegate isKindOfClass:[NSNull class]] == NO)
{
[alertDelegate alertWithTag:tag buttonActionWithButtonIndex:buttonIndex withButtonTitle:buttonTitle];
}
}
#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 alertButtonActionsWithTag:alertView.tag buttonIndex:buttonIndexToPass withButtonTitle:[alertView buttonTitleAtIndex:buttonIndex] alertDelegate:[_arrayAlertDelegates lastObject]];
}
#pragma mark - Action sheet
-(void)addActionSheetDelegatesToArray:(id)actionSheetDelegate
{
if (actionSheetDelegate)
{
[_arrayActionSheetDelegates addObject:actionSheetDelegate];
}
else
{
[_arrayActionSheetDelegates addObject:[NSNull null]];
}
}
-(void)showActionSheetWithTag:(NSInteger)tag title:(NSString *)title arrayOfOtherButtonTitles:(NSArray*)arrayButtonTitles cancelButtonTitle:(NSString*)cancelButtonTitle presentInViewController:(UIViewController*)viewController delegate:(id)actionSheetDelegate
{
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 actionButtonActionsWithTag:tag buttonIndex:[arrayButtonTitles indexOfObject:action.title]+1 withButtonTitle:action.title actionSheetDelegate:actionSheetDelegate];
}];
[alertController addAction:action];
}
if (cancelButtonTitle)
{
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[self actionButtonActionsWithTag:tag buttonIndex:0 withButtonTitle:action.title actionSheetDelegate:actionSheetDelegate];
}];
[alertController addAction:actionCancel];
}
[viewController presentViewController:alertController animated:YES completion:^{
[self addActionSheetDelegatesToArray:actionSheetDelegate];
}];
}
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.tag = tag;
[actionSheet showInView:viewController.view];
[self addActionSheetDelegatesToArray:actionSheetDelegate];
}
}
-(void)actionButtonActionsWithTag:(NSInteger)tag buttonIndex:(NSInteger)buttonIndex withButtonTitle:(NSString*)buttonTitle actionSheetDelegate:(id<ActionSheetDelegate>)actionSheetDelegate
{
[_arrayActionSheetDelegates removeLastObject];
if (actionSheetDelegate && [actionSheetDelegate isKindOfClass:[NSNull class]] == NO)
{
[actionSheetDelegate actionSheetWithTag:tag buttonActionWithButtonIndex:buttonIndex withButtonTitle:buttonTitle];
}
}
#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 actionButtonActionsWithTag:actionSheet.tag buttonIndex:buttonIndexToPass withButtonTitle:[actionSheet buttonTitleAtIndex:buttonIndex] actionSheetDelegate:[_arrayActionSheetDelegates lastObject]];
}
@end