Pandurang Yachwad Pandurang Yachwad - 11 months ago 64
Swift Question

How to set default text for addTextField in UIAlertController in Swift

Below is the the code I tried. textField.placeholder works perfectly, I need to set default text for textfield created using addTextField in UIAlertController.

let getPinNameAlert = UIAlertController(title: title, message: message, preferredStyle: .alert)
getPinNameAlert.addTextField(configurationHandler: { (textField) -> Void in
// textField.placeholder = "Pin Name"
textField.text = "Current Pin Name"
textField.keyboardType = UIKeyboardType.default
textField.clearsOnBeginEditing = true

Answer Source

Remove the line textField.clearsOnBeginEditing = true

By default, the alert automatically focuses on the text field, that is why the text you set is being cleared.