How to make UIButton make UITextField start editing text

I have a custom field that allows the user to enter text in a UITextField. To the left of the UITextField is the UIButton. Something like that:

Enter your name: [name input field]

UIButton has the text "ENTER YOUR NAME:", and UITextField is the name input field.

When the user removes the UITextField, the keyboard is displayed on the device, as expected. However, if the user selects the text for "ENTER YOUR NAME": nothing happens. It is also expected. I need to tell UITextField that it should open the keyboard and start editing if UIButton is pressed.

(I’ve already thought about UITextField borders spanning UIButton. However, due to text alignment in UITextField I cannot do things like left or right excuse. It should remain like centered text.) Thanks for any help you can provide.

+5
source share
2 answers

What you can do is bind the UIButton touchDownInside action method (in Interface Builder) that calls becomeFirstResponderin the text box. Then the text box starts editing.

+12
source
Tim, this did the trick. In the interface builder, I made UIButton respond to "Touch Up Inside", and then I created a method that just did:
- (IBAction)myUIButtonAction:(id)sender {
[myUITextField becomeFirstResponder];
}

! , . [ !]

+5

All Articles