Skip to content

Instantly share code, notes, and snippets.

@foxicode
Created September 15, 2020 06:59
Show Gist options
  • Select an option

  • Save foxicode/b0fe048da13efa5ee2969064632e4be4 to your computer and use it in GitHub Desktop.

Select an option

Save foxicode/b0fe048da13efa5ee2969064632e4be4 to your computer and use it in GitHub Desktop.
Ask a general question in Swift
import UIKit
extension UIViewController {
func ask(title: String?, question: String?, positiveButtonTitle: String = "Yes", negativeButtonTitle: String = "No", isDangerousAction: Bool = false, delegate: @escaping (_ agreed: Bool) -> Void) {
let alert = UIAlertController(title: title, message: question, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: positiveButtonTitle, style: isDangerousAction ? .destructive : .default) { (_) in
delegate(true)
})
alert.addAction(UIAlertAction(title: negativeButtonTitle, style: .cancel) { (_) in
delegate(false)
})
present(alert, animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment