I have an UIAlertController with a textField
showing on my app. The textField
is selected (the marker is blinking in it) and .becomeFirstResponder
is set on the textField
. But for some reason, the soft-keyboard
isn't showing. I printed a boolean isFirstResponder
and it returned false
.
I read somewhere that it has to be trigged in viewDidLoad
, but that's not possible in this case since the alert is shown by pressing a button
from a function
that is outside of viewDidLoad
Here's my code:
func verificationPopup(title: String, message: String, codeShouldBeVerified: Bool, context: UIViewController, callback: @escaping() -> Void) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
//THIS IS THE RELEVANT PART
if(codeShouldBeVerified) {
alert.addTextField(configurationHandler: { textField in
textField.placeholder = "Fyra siffror"
textField.textAlignment = NSTextAlignment.center
textField.delegate = self as? UITextFieldDelegate
textField.becomeFirstResponder()
print("HERE", textField.isFirstResponder)
})
alert.addAction(UIAlertAction(title: "Jag fick ingen kod", style: .default, handler: { action in
callback()
}))
}
else {
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
callback()
//self.verifyNumber()
}))
alert.addAction(UIAlertAction(title: "Avbryt", style: .cancel, handler: {
action in
print("CAAAANCEL")
}))
}
context.present(alert, animated: true)
}
Seems odd that .isFirstResponder
returns false when I just set it. What's going on here?