1

I am new to iPhone programming. I have two textfields in iPhone with numberPad Keyboard type and i am trying to implement a simple logic that on typing a single digit using numberPadKeyBoard, the control should shift to next textField i.e. second textfield should become FirstResponder. I don't know how to implement this. Please guys any help would be appreciated.

Nilesh_iOSDev
  • 671
  • 4
  • 18
Vik
  • 488
  • 2
  • 10
  • 19

3 Answers3

2
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

if(textField == urFirstTextField) { 
[urFirstTextField resignFirstResponder];
[urSecondtextField becomeFirstResponder];

}

}

UPDATE

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
   if(textField != urFirstTextField)
    {
        [textField resignFirstResponder];
        [urFirstTextField becomeFirstResponder];
        return NO;
    }
       return YES;
}
visakh7
  • 26,380
  • 8
  • 55
  • 69
  • @7KV7 - Thanks its working properly. One more question. Say I have three textfields instead of two and I want first textField to be my first responder every time, no matter on which text field user taps first. Where should i put the code for that in the same function? and how? - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (textField != textField_1) {[textField becomeFirstResponder];} if (textField == textField_1 && textField.text.length == 1) { } Is this the right approach? – Vik Jun 20 '11 at 11:18
  • @7kv7 - this update is not working. Its making the same text field as the first responder which is tapped by user. so, not making first text field first responder every time which is desired Can you tell me what is wrong in my approach? any ideas? – Vik Jun 20 '11 at 11:56
  • @7kv7- Im sorry. my bad. it doesn't crash. i updated my comment please check it – Vik Jun 20 '11 at 12:00
1

Suppose you have 2 text field textField1 and textField2 then implement the delgate methods of the UITextFieldDelegate as

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
      if(textField == textField1)
           {
              [textField1 resignFirstResponder];
              [textField2 becomeFirstResponder];
           }
      return YES;
}
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
0

go through the delegate method of UITextField named "textField:shouldChangeText:inRange:" ..... check the length of your textField and make second textField as first responder before returning YES;

Or you can play with a lot of other delegate methods defined

Amit Singh
  • 8,383
  • 4
  • 28
  • 31