Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a UIButton cause a UITextField to begin editing text

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

ENTER YOUR NAME: [name input field]

In the about the UIButton has the text "ENTER YOUR NAME:" and the UITextField is the [name input field].

When the user taps the UITextField, the keyboard is displayed on the device, as expected. However, if the user taps the text for "ENTER YOUR NAME:" nothing happens. This is also expected. I need to tell the UITextField that it should bring up the keyboard and begin editing if the UIButton is pressed.

(I have already thought about making the bounds of the UITextField encompass the UIButton. However, due to the alignment of the text in the UITextField, I can't do things like left or right justification. It must remain as centered text.) Thanks for any assistance you can provide.

like image 918
kspeacock Avatar asked Aug 13 '09 22:08

kspeacock


1 Answers

What you can do is tie a method to the UIButton's touchDownInside action (in Interface Builder) that calls becomeFirstResponder on the text field. The text field will then begin editing.

like image 129
Tim Avatar answered Sep 21 '22 18:09

Tim