forked from thellimist/EZAlertController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEZAlertController.swift
137 lines (110 loc) · 6.16 KB
/
EZAlertController.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
//
// EZAlertView.swift
// EZAlertView
//
// Created by Furkan Yilmaz on 11/11/15.
// Copyright © 2015 Furkan Yilmaz. All rights reserved.
//
import UIKit
public class EZAlertController {
//==========================================================================================================
// MARK: - Singleton
//==========================================================================================================
class var instance : EZAlertController {
struct Static {
static let inst : EZAlertController = EZAlertController ()
}
return Static.inst
}
//==========================================================================================================
// MARK: - Private Functions
//==========================================================================================================
private func topMostController() -> UIViewController? {
var presentedVC = UIApplication.sharedApplication().keyWindow?.rootViewController
while let pVC = presentedVC?.presentedViewController
{
presentedVC = pVC
}
if presentedVC == nil {
print("EZAlertController Error: You don't have any views set. You may be calling in viewdidload. Try viewdidappear.")
}
return presentedVC
}
//==========================================================================================================
// MARK: - Class Functions
//==========================================================================================================
public class func alert(title: String) -> UIAlertController {
return alert(title, message: "")
}
public class func alert(title: String, message: String) -> UIAlertController {
return alert(title, message: message, acceptMessage: "OK") { () -> () in
// Do nothing
}
}
public class func alert(title: String, message: String, acceptMessage: String, acceptBlock: () -> ()) -> UIAlertController {
return alert(title,message: message,acceptMessage: acceptMessage,acceptBlock: acceptBlock,textFieldCompletitionHandler: nil)
}
public class func alert(title: String, message: String, acceptMessage: String, acceptBlock: () -> (), textFieldCompletitionHandler : (UITextField -> Void)? ) -> UIAlertController {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
let acceptButton = UIAlertAction(title: acceptMessage, style: .Default, handler: { (action: UIAlertAction) in
acceptBlock()
})
alert.addAction(acceptButton)
if (textFieldCompletitionHandler != nil ) {
alert.addTextFieldWithConfigurationHandler(textFieldCompletitionHandler)
}
instance.topMostController()?.presentViewController(alert, animated: true, completion: nil)
return alert
}
public class func alert(title: String, message: String, buttons:[String], tapBlock:((UIAlertAction,Int) -> Void)?) -> UIAlertController{
return alert(title, message: message, buttons: buttons, tapBlock: tapBlock, textFieldCompletitionHandler: nil)
}
public class func alert(title: String, message: String, buttons:[String], tapBlock:((UIAlertAction,Int) -> Void)?, textFieldCompletitionHandler : [(UITextField -> Void)?]?) -> UIAlertController{
let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert, buttons: buttons, tapBlock: tapBlock, textFieldsCompletitionHandler: textFieldCompletitionHandler)
instance.topMostController()?.presentViewController(alert, animated: true, completion: nil)
return alert
}
public class func actionSheet(title: String, message: String, sourceView: UIView, actions: [UIAlertAction]) -> UIAlertController {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.ActionSheet)
for action in actions {
alert.addAction(action)
}
alert.popoverPresentationController?.sourceView = sourceView
alert.popoverPresentationController?.sourceRect = sourceView.bounds
instance.topMostController()?.presentViewController(alert, animated: true, completion: nil)
return alert
}
public class func actionSheet(title: String, message: String, sourceView: UIView, buttons:[String], tapBlock:((UIAlertAction,Int) -> Void)?) -> UIAlertController{
let alert = UIAlertController(title: title, message: message, preferredStyle: .ActionSheet, buttons: buttons, tapBlock: tapBlock, textFieldsCompletitionHandler: nil)
alert.popoverPresentationController?.sourceView = sourceView
alert.popoverPresentationController?.sourceRect = sourceView.bounds
instance.topMostController()?.presentViewController(alert, animated: true, completion: nil)
return alert
}
}
private extension UIAlertController {
convenience init(title: String?, message: String?, preferredStyle: UIAlertControllerStyle, buttons:[String], tapBlock:((UIAlertAction,Int) -> Void)?, textFieldsCompletitionHandler : [(UITextField -> Void)?]? ) {
self.init(title: title, message: message, preferredStyle:preferredStyle)
var buttonIndex = 0
for buttonTitle in buttons {
let action = UIAlertAction(title: buttonTitle, preferredStyle: .Default, buttonIndex: buttonIndex, tapBlock: tapBlock)
buttonIndex++
self.addAction(action)
}
if let textFieldsCompletitionHandler = textFieldsCompletitionHandler {
for compHandler in textFieldsCompletitionHandler {
addTextFieldWithConfigurationHandler(compHandler)
}
}
}
}
private extension UIAlertAction {
convenience init(title: String?, preferredStyle: UIAlertActionStyle, buttonIndex:Int, tapBlock:((UIAlertAction,Int) -> Void)?) {
self.init(title: title, style: style) {
(action:UIAlertAction) in
if let block = tapBlock {
block(action,buttonIndex)
}
}
}
}