Created
December 27, 2020 15:17
-
-
Save srstanic/823f9e1691902672450b08e811158e76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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