48

How do you programmatically set focus to some UITextField in iOS?

When you tap on a textfield, the keyboard pops up to begin editing the textfield.

How can you give focus to a textfield and bring up the keyboard without requiring the user to tap on the textfield?

pkamb
  • 33,281
  • 23
  • 160
  • 191
Ravi kumar
  • 601
  • 1
  • 6
  • 7
  • 4
    for focus use [textfield becomeFirstResponder] – Narayana Rao Routhu Sep 23 '11 at 07:09
  • According to your requirement..if you want to add click event !! you can set Single TapGesture which is alternative for click event.. [YourtextField addGestureRecognizer:tapGesture]; dont forget to declare tap gesture before adding !! – Parvez Belim May 21 '13 at 10:08
  • 7
    It's not difficult at all to tell what's being asked here, and it's a completely legit question. Closing questions like this does not help the community. – devios1 Aug 02 '13 at 21:40
  • @chaiguy The question shows no research and no previous attempt. By the new guidelines this would be still put on-hold as Offtopic – madth3 Aug 02 '13 at 22:16
  • 9
    This is an absolutely valid question. The user is not a native English writer - but so what!??! it's an important basic question regarding iOS development - indeed the answer is "not that easy". – Fattie Apr 29 '14 at 10:55

3 Answers3

69

Make the textField the first responder; this will also popup the keyboard:

[self.textField becomeFirstResponder];

And just some more typical example code which may help someone,

in your storyboard, click on one of the text fields, and set the outlets-delegate to be the class in question. In that class, in the .h file make sure that you declare that it is a UITextFieldDelegate

@interface Login : UIViewController <UITextFieldDelegate>

Then in the .m file, you could have this for example...

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    if ( textField == self.loginEmail ) { [self.loginPassword becomeFirstResponder]; }
    if ( textField == self.loginPassword ) { [self yourLoginRoutine]; return YES; }
    return YES;
}

When a user clicks the "Next" button on the virtual keyboard - on the 'email' field - it will correctly move you to the password field. When a user clicks the "Next" button on the virtual keyboard - on the 'password' field - it will correctly call your yourLoginRoutine.

(On the storyboard, on the attributes inspector, notice that you can select the text you want on the Return key ... here "Next" would work nicely on the email field and "Done" would work nicely on the password field.)

pkamb
  • 33,281
  • 23
  • 160
  • 191
rckoenes
  • 69,092
  • 8
  • 134
  • 166
26

For changing focus on the fly:

[yourTextField becomeFirstResponder];
pkamb
  • 33,281
  • 23
  • 160
  • 191
EXC_BAD_ACCESS
  • 2,699
  • 2
  • 21
  • 25
  • this site is only getting input and placed to input to the another textfield. i asked how to set focus(like setOnFocus in android) in textfield and hw to create click event for textfield in iphone. – Ravi kumar Sep 23 '11 at 07:14
  • 1
    Add the TouchUpInside event for TextField – EXC_BAD_ACCESS Sep 23 '11 at 07:16
0

For Swift, you can use:

theTextFieldYouWant.becomeFirstResponder()

Diego Salcedo
  • 83
  • 1
  • 7