Skip to content

Instantly share code, notes, and snippets.

let actionSheet = UIAlertController.actionSheet(actions: [
.Default(title: "Default", handler: { _ in print("tap")}),
.Destructive(title: "Destructive", handler: { _ in print("tap")}),
.Cancel(title: "Cancel", handler: nil)
])
presentViewController(alert, animated:true, completion:nil)
protocol SwiftyAlertController {
static func actionSheet(title: String?, message: String?, actions: [EasyAlertControllerAction]) -> UIAlertController
static func alert(title: String?, message: String?, actions: [EasyAlertControllerAction]) -> UIAlertController
}
extension SwiftyAlertController where Self: UIAlertController {
static func actionSheet(title: String? = nil, message: String? = nil, actions: [EasyAlertControllerAction]) -> UIAlertController {
let alert = UIAlertController(title: title, message: message, preferredStyle: .ActionSheet)
actions.forEach { alert.addAction($0.action) }
extension SwiftyActions {
var action: UIAlertAction {
switch self {
case .Default(let title, let handler): return UIAlertAction(title: title, style: .Default, handler: handler)
case .Cancel(let title, let handler): return UIAlertAction(title: title, style: .Cancel, handler: handler)
case .Destructive(let title, let handler): return UIAlertAction(title: title, style: .Destructive, handler: handler)
}
}
}
enum SwiftyActions {
case Default(title: String?, handler:((UIAlertAction) -> Void)?)
case Cancel(title: String?, handler:((UIAlertAction) -> Void)?)
case Destructive(title: String?, handler:((UIAlertAction) -> Void)?)
}
@0ber
0ber / Swifty_UIAlerViewController.swift
Last active September 5, 2016 19:21
example to create actionSheet
let actionSheet = UIAlertController(title: "title", message: "message", preferredStyle: .ActionSheet)
let action = UIAlertAction(title: "default", style: .Default, handler: {_ in print("tap")})
let cancel = UIAlertAction(title: "cancel", style: .Cancel, handler: {_ in print("tap")})
let destructive = UIAlertAction(title: "action", style: .Destructive, handler: {_ in print("tap")})
actionSheet.addAction(action)
actionSheet.addAction(cancel)
actionSheet.addAction(destructive)