UITextField, automatically move to next after 1 character

后端 未结 6 723

Scenario: I have 4 UITextFields that only accept 1 character. Easy.

Problem: After I enter the 1 character, I want the next TextField to become active automatically with

6条回答
  •  名媛妹妹
    2021-01-31 12:10

    Although it is an old question, I just came cross it and I came with simpler solution. assuming we doing this for pass code so each box (UITextField) max length is one char.

    - (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
        if (![string isEqualToString:@""]) {
            textField.text = string;
            if ([textField isEqual:self.txtField1]) {
                [self.txtField2 becomeFirstResponder];
            }else if ([textField isEqual:self.txtField2]){
                [self.txtField3 becomeFirstResponder];
            }else if ([textField isEqual:self.txtField3]){
                [self.txtField4 becomeFirstResponder];
            }else{
                [textField resignFirstResponder];
            }
            return NO;
        }
        return YES;
    }
    
    -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
        if (textField.text.length > 0) {
            textField.text = @"";
        }
        return YES;
    }
    

提交回复
热议问题