-
-
Save riteshhgupta/ce957c5ecfb01d5f47827496cf1d7e4d 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
// 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