-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwiftAlertController.swift
325 lines (237 loc) · 9.93 KB
/
SwiftAlertController.swift
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//
// SwiftAlertController.swift
// Copyright (c) 2016, zahlz
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import UIKit
extension UIAlertController {
/**
Create an alert style `UIAlertController`
- parameter name: Title for the `UIAlertController`
- parameter message: Message to display (default: `nil`)
- parameter acceptMessage: Message in the accept button (default: "OK")
- parameter handler: Handler for the button click (default: `nil`)
- returns: An `UIAlertController`
*/
class func alert(
_ name: String,
message: String? = nil,
acceptMessage: String = "OK",
handler: ((UIAlertAction) -> Void)? = nil
) -> UIAlertController {
return UIAlertController.alert(name, message: message, actions: [UIAlertAction(title: acceptMessage, style: .cancel, handler: handler)])
}
/**
Create an alert style `UIAlertController`
- parameter name: Title for the `UIAlertController`
- parameter message: Message to display (default: `nil`)
- parameter actions: Array with the `UIAlertAction`s
- returns: An `UIAlertController`
*/
class func alert(
_ name: String,
message: String? = nil,
actions: [UIAlertAction]
) -> UIAlertController {
let alertController = UIAlertController(title: name, message: message, preferredStyle: .alert)
actions.forEach(alertController.addAction)
return alertController
}
/**
Create an alert style `UIAlertController`
- parameter name: Title for the `UIAlertController`
- parameter message: Message to display (default: `nil`)
- parameter actionHandler: Closure which returns the `UIAlertAction`s
- returns: An `UIAlertController`
*/
class func alert(
_ name: String,
message: String? = nil,
actionHandler: (() -> [UIAlertAction])
) -> UIAlertController {
return UIAlertController.alert(name, message: message, actions: actionHandler())
}
/**
Create an actionSheet style `UIAlertController`
- parameter name: Title for the `UIAlertController`
- parameter message: Message to display (default: `nil`)
- parameter actions: Array with the `UIAlertAction`s (default: `nil`)
- returns `UIAlertController`
*/
class func sheet(
_ name: String,
message: String? = nil,
actions: [UIAlertAction]? = nil
) -> UIAlertController {
let alertController = UIAlertController(title: name, message: message, preferredStyle: .actionSheet)
actions?.forEach(alertController.addAction)
return alertController
}
/**
Create an actionSheet style `UIAlertController`
- parameter name: Title for the `UIAlertController`
- parameter message: Message to display (default: `nil`)
- parameter actionHandler: Closure which returns the `UIAlertAction`s
- returns `UIAlertController`
*/
class func sheet(
_ name: String,
message: String? = nil,
actionHandler: (() -> [UIAlertAction])
) -> UIAlertController {
return UIAlertController.sheet(name, message: message, actions: actionHandler())
}
/**
Adds an action to an `UIAlertController` and is chainable
- parameter title: Title for the action
- parameter style: Style of the action (default: `.default`)
- parameter handler: Handler for the action (default: `nil`)
- returns: UIAlertController with added action
*/
func action(
title: String,
style: UIAlertActionStyle = .default,
handler: ((UIAlertAction) -> Void)? = nil
) -> UIAlertController {
addAction(title: title, style: style, handler: handler)
return self
}
/**
Adds an okay action to the UIAlertController
- parameter handler: Handler for the action (default: `nil`)
- returns UIAlertController with added action
*/
func okable(
handler: ((UIAlertAction) -> Void)? = nil
) -> UIAlertController {
addOk(handler: handler)
return self
}
/**
Add a cancel action to the UIAlertViewController
- parameter handler: Handler for the action (default: `nil`)
- returns: UIAlertController with added action
*/
func cancelable(
handler: ((UIAlertAction) -> Void)? = nil
) -> UIAlertController {
addCancel(handler: handler)
return self
}
/**
Adds an action to an UIAlertController
- parameter title: Title for the action
- parameter style: Style of the action (default: `.default`)
- parameter handler: Handler for the action (default: `nil`)
*/
func addAction(
title: String,
style: UIAlertActionStyle = .default,
handler: ((UIAlertAction) -> Void)? = nil
) {
let action = UIAlertAction(title: title, style: style, handler: handler)
addAction(action)
}
/**
Adds an okay action to the UIAlertController
- parameter handler: Handler for the action (default: `nil`)
*/
func addOk(handler: ((UIAlertAction) -> Void)? = nil) {
addAction(title: NSLocalizedString("OK", comment: ""), style: .default, handler: handler)
}
/**
Add a cancel action to the UIAlertViewController
- parameter handler: Handler for the action (default: `nil`)
*/
func addCancel(handler: ((UIAlertAction) -> Void)? = nil) {
addAction(
title: NSLocalizedString("Cancel", comment: ""),
style: .cancel,
handler: handler
)
}
}
extension UIAlertAction {
/**
Create and return an action with the specified title and behavior.
Actions are enabled by default when you create them.
- parameter title: The text to use for the button title.
The value you specify should be localized for the user’s current language.
This parameter must not be nil, except in a tvOS app
where a nil title may be used with cancel. (default: `nil`)
- parameter handler: A block to execute when the user selects the action.
This block has no return value and takes the selected action object as its only parameter.
(default: `nil`)
*/
convenience init(
title: String?,
handler: ((UIAlertAction) -> Void)? = nil
) {
self.init(title: title, style: .default, handler: handler)
}
/**
Appends an UIAlertAction to this one and returns a array of UIAlertActions
- parameter title: The text to use for the button title.
The value you specify should be localized for the user’s current language.
This parameter must not be nil, except in a tvOS app
where a nil title may be used with cancel.
- parameter style: Additional styling information to apply to the button.
Use the style information to convey the type of action that is performed by the button.
For a list of possible values, see the constants in UIAlertActionStyle.
(default: `.default`)
- parameter handler: A block to execute when the user selects the action.
This block has no return value and takes the selected action object as its only parameter.
(default: `nil`)
- returns: Array with the `UIAlertAction`s
*/
func appending(
title: String,
style: UIAlertActionStyle = .default,
handler: ((UIAlertAction) -> Void)? = nil
) -> [UIAlertAction] {
return [self, UIAlertAction(title: title, style: style, handler: handler)]
}
}
extension Collection where Iterator.Element == UIAlertAction {
/**
Appends an UIAlertAction to this array and returns a array of UIAlertActions
- parameter title: The text to use for the button title.
The value you specify should be localized for the user’s current language.
This parameter must not be nil, except in a tvOS app
where a nil title may be used with cancel.
- parameter style: Additional styling information to apply to the button.
Use the style information to convey the type of action that is performed by the button.
For a list of possible values, see the constants in UIAlertActionStyle.
(default: `.default`)
- parameter handler: A block to execute when the user selects the action.
This block has no return value and takes the selected action object as its only parameter.
(default: `nil`)
- returns: Array with the `UIAlertAction`s
*/
func appending(
title: String,
style: UIAlertActionStyle = .default,
handler: ((UIAlertAction) -> Void)? = nil
) -> [UIAlertAction] {
return self + [UIAlertAction(title: title, style: style, handler: handler)]
}
}