-
Notifications
You must be signed in to change notification settings - Fork 49
/
EZAlertController.swift
125 lines (102 loc) · 4.96 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
//
// EZAlertView.swift
// EZAlertView
//
// Created by Furkan Yilmaz on 11/11/15.
// Copyright © 2015 Furkan Yilmaz. All rights reserved.
//
import UIKit
open class EZAlertController {
//==========================================================================================================
// MARK: - Singleton
//==========================================================================================================
class var instance : EZAlertController {
struct Static {
static let inst : EZAlertController = EZAlertController ()
}
return Static.inst
}
//==========================================================================================================
// MARK: - Private Functions
//==========================================================================================================
fileprivate func topMostController() -> UIViewController? {
var presentedVC = UIApplication.shared.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
//==========================================================================================================
@discardableResult
open class func alert(_ title: String) -> UIAlertController {
return alert(title, message: "")
}
@discardableResult
open class func alert(_ title: String, message: String) -> UIAlertController {
return alert(title, message: message, acceptMessage: "OK", acceptBlock: {
// Do nothing
})
}
@discardableResult
open class func alert(_ title: String, message: String, acceptMessage: String, acceptBlock: @escaping () -> ()) -> UIAlertController {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
let acceptButton = UIAlertAction(title: acceptMessage, style: .default, handler: { (action: UIAlertAction) in
acceptBlock()
})
alert.addAction(acceptButton)
instance.topMostController()?.present(alert, animated: true, completion: nil)
return alert
}
@discardableResult
open class func alert(_ title: String, message: String, buttons:[String], tapBlock:((UIAlertAction) -> Void)?) -> UIAlertController{
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert, buttons: buttons, tapBlock: tapBlock)
instance.topMostController()?.present(alert, animated: true, completion: nil)
return alert
}
@discardableResult
open class func actionSheet(_ title: String, message: String, sourceView: UIView, actions: [UIAlertAction]) -> UIAlertController {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.actionSheet)
for action in actions {
alert.addAction(action)
}
alert.popoverPresentationController?.sourceView = sourceView
alert.popoverPresentationController?.sourceRect = sourceView.bounds
instance.topMostController()?.present(alert, animated: true, completion: nil)
return alert
}
@discardableResult
open class func actionSheet(_ title: String, message: String, sourceView: UIView, buttons:[String], tapBlock:((UIAlertAction) -> Void)?) -> UIAlertController{
let alert = UIAlertController(title: title, message: message, preferredStyle: .actionSheet, buttons: buttons, tapBlock: tapBlock)
alert.popoverPresentationController?.sourceView = sourceView
alert.popoverPresentationController?.sourceRect = sourceView.bounds
instance.topMostController()?.present(alert, animated: true, completion: nil)
return alert
}
}
private extension UIAlertController {
convenience init(title: String?, message: String?, preferredStyle: UIAlertController.Style, buttons:[String], tapBlock:((UIAlertAction) -> Void)?) {
self.init(title: title, message: message, preferredStyle:preferredStyle)
var buttonIndex = 0
for buttonTitle in buttons {
let action = UIAlertAction(title: buttonTitle, style: .default, handler: tapBlock)
buttonIndex += 1
self.addAction(action)
}
}
}
private extension UIAlertAction {
convenience init(title: String?, preferredStyle: UIAlertAction.Style, buttonIndex:Int, tapBlock:((UIAlertAction,Int) -> Void)?) {
self.init(title: title, style: preferredStyle) {
(action:UIAlertAction) in
if let block = tapBlock {
block(action,buttonIndex)
}
}
}
}