Skip to content

Instantly share code, notes, and snippets.

@riteshhgupta
Last active August 21, 2019 09:00
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save riteshhgupta/ce957c5ecfb01d5f47827496cf1d7e4d to your computer and use it in GitHub Desktop.
Save riteshhgupta/ce957c5ecfb01d5f47827496cf1d7e4d to your computer and use it in GitHub Desktop.
// Snippet
typealias AlertActionHandler = ((UIAlertAction) -> Void)
extension UIAlertControllerStyle {
func controller(title: String, message: String, actions: [UIAlertAction]) -> UIAlertController {
let _controller = UIAlertController(
title: title,
message: message,
preferredStyle: self
)
actions.forEach { _controller.addAction($0) }
return _controller
}
}
extension String {
func alertAction(style: UIAlertActionStyle = .default, handler: AlertActionHandler? = nil) -> UIAlertAction {
return UIAlertAction(title: self, style: style, handler: handler)
}
}
// Usage
let noInternetAlertController = UIAlertControllerStyle.alert.controller(
title: "No Internet",
message: "Something went wrong, please try again!",
actions: [
"Dismiss".alertAction(),
"Retry".alertAction { _ in /* retry logic */ }
])
viewController.present(noInternetAlertController, animated: true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment