-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlertController.swift
102 lines (88 loc) · 3.79 KB
/
AlertController.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
// Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import Foundation
import UIKit
extension UIAlertAction {
/// Action types most commonly used
public enum ActionType {
///Ok Option
case ok
/// Default Cancel Option
case cancel
/// Destructive action with custom title
case destructive(String)
/// Custom action with title and style
case custom(String, UIAlertActionStyle)
/**
Creates the action instance for UIAlertController
- parameter handler: Call Back function
- returns UIAlertAction Instance
*/
public func action(handler: ((String) -> Void)? = nil) -> UIAlertAction {
//Default value
var actionStyle = UIAlertActionStyle.default
var title = ""
// Action configuration based on the action type
switch self {
case .cancel:
actionStyle = .cancel
title = "Cancel"
case .destructive(let optionTitle):
title = optionTitle
actionStyle = .destructive
case .custom(let optionTitle, let style):
title = optionTitle
actionStyle = style
default:
title = "OK"
}
//Creating UIAlertAction instance
return UIAlertAction(title:title, style:actionStyle) { nativeAction in
if let handler = handler {
handler(title)
}
}
}
}
}
extension UIAlertController {
/**
Creates the alert view controller using the actions specified
- parameter title: Title of the alert.
- parameter message: Alert message body.
- parameter actions: Variable numbre of actions as an Array of actionType values.
- parameter style: UIAlertControllerStyle enum value
- parameter handler: Handler block/closure for the clicked option.
*/
convenience init(title: String,
message: String,
actions: UIAlertAction.ActionType?...,
style: UIAlertControllerStyle = .alert,
handler: ((String) -> Swift.Void)? = nil) {
//initialize the contoller (self) instance
self.init(title: title, message: message, preferredStyle: style)
if actions.isEmpty {
addAction(UIAlertAction.ActionType.ok.action(handler: handler))
} else {
//Fetching actions specidied by the user and adding actions accordingly
for actionType in actions {
addAction((actionType?.action(handler: handler))!)
}
}
}
}