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?
There are circumstances when it's normal that the soft keyboard won't come up.
(a) when there's a Bluetooth keyboard attached to a real device, or
(b) when a hardware keyboard is being simulated in the device simulator (menu: Hardware->Keyboard->Connect Hardware Keyboard).
I know this for a long time and still sometimes it happens to me that I get confused why the keyboard won't come up until I remember to check (mostly when using the simulator). It's easy to miss so make sure that this is not the case here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.