Skip to content

Instantly share code, notes, and snippets.

@srstanic
Created December 27, 2020 15:17
Show Gist options
  • Save srstanic/823f9e1691902672450b08e811158e76 to your computer and use it in GitHub Desktop.
Save srstanic/823f9e1691902672450b08e811158e76 to your computer and use it in GitHub Desktop.
typealias AlertActionStyle = UIAlertAction.Style
extension UIAlertController {
static func create(
style: UIAlertController.Style,
title: String?,
message: String?,
actions: [AlertAction] = [],
onDismiss: VoidHandler? = nil
) -> UIAlertController {
let alertViewController = UIAlertController(title: title, message: message, preferredStyle: style)
let nativeActions = actions.map { (alertAction) -> UIAlertAction in
let handler: ((UIAlertAction) -> Void)? = { _ in
alertAction.handler?()
onDismiss?()
}
return UIAlertAction(
title: alertAction.title,
style: alertAction.style,
handler: handler
)
}
for nativeAction in nativeActions {
alertViewController.addAction(nativeAction)
}
return alertViewController
}
static func createDialog(
title: String?,
message: String?,
actions: [AlertAction],
onDismiss: VoidHandler? = nil
) -> UIAlertController {
return create(style: .alert, title: title, message: message, actions: actions, onDismiss: onDismiss)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment