UIAlertController是蘋果iOS 8以后推出的彈窗視圖井濒,其用來取代UIAlertView燃箭,官方也建議在iOS 9之后棄用后者昏苏,
前者相比后者來說寫法簡單易懂肴盏,但是代碼仍算比較多芋浮,所以我使用了block的方式將其簡化封裝抱环,方便全局和多次使用。也方便以后各種替換這個(gè)彈窗。
特意也使用@objc 來提供給OC使用镇草,這樣就清爽多啦眶痰。
其中也添加了適配iPad的代碼。
先來看看使用效果:
// Sheet
let alertController = WSAlertController.alertSheet(title: "Sheet").add(title: "title1", style: .default) {
// your code
}.add(title: "title2", style: .destructive) {
// your code
}.add(title: "title3", style: .cancel) {
// your code
}.finish()
// Alert
let alertController = WSAlertController.alertAlert(title: "title", message: "message", okTitle: "ok", cancelTitle: "cancel") {
// your code
}
// Input
let alertController = WSAlertController.alertInputViews(title: "title", message: "message", placeholders: ["請輸入賬號","請輸入密碼"]) { (inputTextArray) in
// your code
}
是不是簡化了許多梯啤。
具體代碼:
import UIKit
///可空的無參無返回值 Block
typealias AlertCompleteOptional = (() -> Swift.Void)?
class WSAlertController: NSObject{
//MARK: 鏈?zhǔn)匠跏蓟椒? private var title: String?
private var actionArray: [(String,UIAlertAction.Style,AlertCompleteOptional)] = []
init(title: String?) {
self.title = title
}
/// 類方法初始化鏈?zhǔn)? @objc class func alertSheet(title: String?) -> WSAlertController{
return WSAlertController(title: title)
}
@discardableResult
@objc func add(title: String, style: UIAlertAction.Style, complete: AlertCompleteOptional) -> WSAlertController{
actionArray.append((title,style,complete))
return self
}
@objc func finish() ->UIAlertController{
return WSAlertController.alertControllerSheet(title: self.title, actionArray: self.actionArray)
}
/// 使用actionSheet樣式的系統(tǒng)AlertControllerSheet封裝
///
/// - Parameters:
/// - title: 標(biāo)題
/// - hitSender: 當(dāng)為ipad設(shè)備時(shí)給予一個(gè)附著的控件
/// - actionArray: 每個(gè)action獨(dú)立元組數(shù)組
/// - Returns: 返回這個(gè)alertController用于顯示出來
private class func alertControllerSheet(title: String?,
actionArray: [(String,UIAlertAction.Style,AlertCompleteOptional)]) -> UIAlertController{
let alertVC = UIAlertController(title: title, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
for action in actionArray{
let alertAction = UIAlertAction(title: action.0, style: action.1, handler: { (_) in
action.2?()
})
alertVC.addAction(alertAction)
}
addPopPresenterView(alertVC: alertVC)
return alertVC
}
//MARK: 類方法
/// 返回alertController 有取消和確定按鈕
///
/// - Parameters:
/// - title: 標(biāo)題
/// - message: 內(nèi)容
/// - okTitle: 確定按鈕的文字
/// - okComplete: 回調(diào)事件
/// - Returns: alertController 實(shí)例
@objc class func alertAlert(title: String?, message: String?, okTitle: String, cancelTitle: String? = nil, okComplete: AlertCompleteOptional) -> UIAlertController{
let alertVC = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
let alertAction = UIAlertAction(title: okTitle, style: .default, handler: { (_) in
if okComplete != nil{
okComplete!()
}
})
let cancel = UIAlertAction(title: cancelTitle ?? "取消", style: .cancel) { (alert: UIAlertAction) -> Void in
}
alertVC.addAction(alertAction)
alertVC.addAction(cancel)
addPopPresenterView(alertVC: alertVC)
return alertVC
}
/// 返回alertController 僅確定按鈕
///
/// - Parameters:
/// - title: 標(biāo)題
/// - message: 內(nèi)容
/// - okTitle: 確定按鈕的文字
/// - okComplete: 回調(diào)事件
/// - Returns: alertController 實(shí)例
@objc class func alertAlert(title: String?, message: String?, okTitle: String, okComplete: AlertCompleteOptional) -> UIAlertController{
let alertVC = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
let alertAction = UIAlertAction(title: okTitle, style: .default, handler: { (_) in
if okComplete != nil{
okComplete!()
}
})
alertVC.addAction(alertAction)
addPopPresenterView(alertVC: alertVC)
return alertVC
}
/// 多個(gè)輸入框的alertController
///
/// - Parameters:
/// - title: 標(biāo)題
/// - message: 內(nèi)容
/// - placeholders: 多少個(gè)提示文字 代表多少個(gè)框
/// - okComplete: 回調(diào)字符串?dāng)?shù)組 數(shù)組內(nèi)容順序是輸入框的順序
public class func alertInputViews(title: String?,
message: String?,
placeholders: [String]?,
okComplete: @escaping ((_ text: [String]) -> Void)) -> UIAlertController{
let alertVC = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
if let placeholderList = placeholders{
for placeholder in placeholderList{
alertVC.addTextField { (textField) in
textField.placeholder = placeholder
}
}
}
let okAction = UIAlertAction(title: "確定", style: UIAlertActionStyle.default) { (action) in
if let textFields = alertVC.textFields{
var inputText: [String] = []
for textfield in textFields{
if let text = textfield.text{
inputText.append(text)
}
}
okComplete(inputText)
}
}
let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel) { (action) in
}
alertVC.addAction(okAction)
alertVC.addAction(cancelAction)
addPopPresenterView(alertVC: alertVC)
return alertVC
}
/// 適配iPad
private class func addPopPresenterView(alertVC: UIAlertController){
if UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad {
let popPresenter = alertVC.popoverPresentationController
if let keywindow = UIApplication.shared.keyWindow{
popPresenter?.sourceView = keywindow
if alertVC.preferredStyle == UIAlertControllerStyle.alert {
popPresenter?.sourceRect = CGRect(x: keywindow.width()/2, y:keywindow.height()/2, width: 0, height: 0)
}else {
popPresenter?.sourceRect = CGRect(x: keywindow.width()/2, y: keywindow.height(), width: 0, height: 0)
}
popPresenter?.permittedArrowDirections = []
}
}
}
}