1

我水平有 4 个文本字段。每个 textFiled 将只允许四个字符。如果用户输入四个字符,则焦点应更改为下一个文本字段,而无需任何用户交互。通过使用下面的代码,我可以切换到下一个 textFiled,但只有在从键盘输入任何字符之后。我的要求是在输入四个字符后自动关注下一个文件。请帮助我。提前致谢。

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

if ( range.location  >= 4 ) {
  textField.text = string;        
    UIResponder* nextResponder = [textField.superview viewWithTag:(textField.tag + 1)];
    if (nextResponder) {           
        [textField resignFirstResponder];            
        [nextResponder becomeFirstResponder];          
    }        
    return NO;
}    
return YES;

}

4

4 回答 4

1

你可以在这个方法中使用这样的东西:

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {

    if(theTextField == firstField)
    {
        [secondField becomeFirstResponder];
        return YES;
    }

    if(theTextField == secondField)
    {
        [thirdField becomeFirstResponder];
        [theTextField resignFirstResponder];
        return YES;
    }

    if(theTextField == thirdField)
    {
        [thirdField becomeFirstResponder];
        [theTextField resignFirstResponder];
        return YES;
    }
    [theTextField resignFirstResponder];
    return YES;
}
于 2012-06-15T06:17:06.110 回答
0
//try to this code 


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  if ([[textField text] length] + [string length] - range.length > 4) {

    textField.text = string;        
   UIResponder* nextResponder = [textField.superview viewWithTag:(textField.tag + 1)];
   if (nextResponder) {           
      [textField resignFirstResponder];            
      [nextResponder becomeFirstResponder];          
  }   

    return YES; 
}

希望对你有帮助

于 2012-06-15T06:38:58.337 回答
0

我认为你需要这样的东西, https://github.com/lashad/PTPasscodeViewController

于 2012-06-15T06:31:44.373 回答
0

尝试更换这个

if ( range.location  >= 4 )

if ( range.location  >= 3 )
于 2012-06-15T06:35:03.703 回答