Created
September 13, 2020 05:07
-
-
Save foxicode/485c63b0cb97fbbfa4df94e3cb6f0d4a to your computer and use it in GitHub Desktop.
Ask user a question in Swift (iOS)
This file contains hidden or 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
| 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