I'm making an app and I want the user to be able to input text without having them actually click on a UITextField (have it open after the game ends for a scoreboard), is there some way to do that?
Asked
Active
Viewed 194 times
0
-
https://stackoverflow.com/questions/17279781/programmatically-tap-a-uitextfield – Ahmad F Feb 18 '20 at 18:09
-
@AhmadF While the answer is the same, that solution doesn't seem to be working in my case. – JustSomeoneWhoCodes Feb 19 '20 at 07:27
-
Then how you accepted the answer? – Ahmad F Feb 19 '20 at 20:09
-
Because it worked when I tested in on another project, so I probably did something wrong other than that line of code. – JustSomeoneWhoCodes Feb 19 '20 at 22:25
2 Answers
1
Yes, just:
<#your_text_field#>.becomeFirstResponder()
after the game ends.
This will produce the "same" effect as a tap on the UITextField
. If this is on a new UIViewController
you can place it on viewDidAppear
, otherwise you can assume everything is already loaded and simply call it anywhere.

Gustavo Vollbrecht
- 3,188
- 2
- 19
- 37
-
Do I need to call something after saying .becomeFirstResponder? I initilized it with nameInput.isHidden = false nameInput.becomeFirstResponder() lbNames[0] = nameInput.text! and the textfield is simply let nameInput: UITextField = UITextField() do I need to do something else? – JustSomeoneWhoCodes Feb 18 '20 at 17:02
1
Call the following in the viewDidLoad
of scoreboard, or whatever the entry point of that view is.
yourTextField.becomeFirstResponder()

emrepun
- 2,496
- 2
- 15
- 33
-
I would assume not, just call yourTextField.isHidden = false right before calling the code in the answer, it should work fine :) – emrepun Feb 18 '20 at 16:52
-
Do I need to call something after saying .becomeFirstResponder? I initilized it with `nameInput.isHidden = false nameInput.becomeFirstResponder() lbNames[0] = nameInput.text!` and the textfield is simply `let nameInput: UITextField = UITextField()` do I need to do something else? – JustSomeoneWhoCodes Feb 18 '20 at 17:01
-
-