Search code examples
iphoneiosuitextfielduitextfielddelegate

Moving the focus of a textfield to another based on conditions


I have 10 textfields, each of which could hold at most one character. When I enter a character in the first textfield, the focus should automatically move to the next textfield and so on. That is, as soon as the first character is entered in a textfield, the focus should shift to the next. That is, the next textfield should become the first responder. I have written the below code, used the textfield delegate method.

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

NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"];

for (int i = 0; i < [string length]; i++) {
    unichar c = [string characterAtIndex:i];
    if([myCharSet characterIsMember:c])
    {
        int previousTag = textField.tag;


        if([textField.text length] > 0)
        {
            if((previousTag == 9) && ([textField10.text length] >0))
            {
                return NO;
            }
            UITextField *tempField=(UITextField *)[self.view viewWithTag:previousTag+1]; 
            if([tempField.text length] > 0){
                [tempField resignFirstResponder];
                return NO;
            }
            [tempField becomeFirstResponder];
            return YES;
        }

    }
    else{
        return NO;
    }
}


return YES;
}

But I am not getting the desired results. When I type a character its entered in the first textfield, but the focus is not shifting to the next, though when I type the 2nd character, it is entered in the next textfield.

Similarly, I need to write a delete function such that when I delete a textfield, the focus automatically shifts to the previous textfield.

Any answers will be appreciated. Thanks.


Solution

  • You can always return NO and change the textField text manually. I guess what you need is something like this.

     - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"];
    int previousTag = textField.tag;
    
    if ([string isEqualToString:@""]) {//backspace button
        if (previousTag==0) {//added to prevent crashing in first tf
            return YES;
        }
      UITextField *tempField2=(UITextField *)[self.view viewWithTag:previousTag-1]; 
    
        textField.text=string;
        [tempField2 becomeFirstResponder];
        return NO;
    
    }
    for (int i = 0; i < [string length]; i++) {
        unichar c = [string characterAtIndex:i];
        if([myCharSet characterIsMember:c])
        {
            if((previousTag == 9) && ([textField10.text length] >0))
            {
                return NO;
            }
    
            UITextField *tempField=(UITextField *)[self.view viewWithTag:previousTag+1]; 
            if([tempField.text length] > 0)
            { 
                textField.text=string;
                [tempField resignFirstResponder];
                return NO;
            }
            textField.text=string;
            [tempField becomeFirstResponder];
            return NO;
    
        }
        else{
            return NO;
        }
    
    
    }
    
    
    return YES;
    }