Skip to content

Instantly share code, notes, and snippets.

@foxicode
Created September 13, 2020 05:07
Show Gist options
  • Select an option

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

Select an option

Save foxicode/485c63b0cb97fbbfa4df94e3cb6f0d4a to your computer and use it in GitHub Desktop.
Ask user a question in Swift (iOS)
import UIKit
extension UIViewController {
func ask(title: String?, question: String?, placeholder: String?, keyboardType: UIKeyboardType = .default, delegate: @escaping (_ answer: String?) -> Void) {
let alert = UIAlertController(title: title, message: question, preferredStyle: .alert)
alert.addTextField { (textField) in
textField.placeholder = placeholder
textField.keyboardType = keyboardType
}
alert.addAction(UIAlertAction(title: "OK", style: .default) { (_) in
let answer = alert.textFields?.first?.text
delegate(answer)
})
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { (_) in
delegate(nil)
})
present(alert, animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment