uiresponder下一个textfield在keyEvent之后成为第一个响应者

时间:2012-06-15 06:11:15

标签: iphone uitextfield uiresponder

我水平地有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 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

尝试替换此

if ( range.location  >= 4 )

if ( range.location  >= 3 )

答案 3 :(得分: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; 
}

希望这会对你有所帮助