将文本字段输入限制为1并将光标隐藏在文本字段上

时间:2012-09-04 07:15:57

标签: iphone uitextfield

在我的应用程序中,我有一个视图,使用4个textfields输入一个4位数的密码。当用户触摸第一个文本字段并输入第一个字符时,控件应自动转到第二个,依此类推。在第4个之后,验证应该发生。还要当用户接触文本框时,用户应该无法看到光标。如何做到这一点?

2 个答案:

答案 0 :(得分:0)

请尝试以下操作:

   - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if( [textField.text length]==0)
    {
        [nextText becomeFirstResponder];
    }

    return YES;
}

答案 1 :(得分:0)

试试这个

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

    if(theTextField == wordTextField1) {
    [wordTextField1 setText:string];
     if(string.length == 1) {
     [wordTextField2 becomeFirstResponder];
    }
     }
    else if(theTextField == wordTextField2) {
    [wordTextField2 setText:string];
    if(string.length == 1) {
    [wordTextField3 becomeFirstResponder];
     }
       }
         else if(theTextField == wordTextField3) {
       [wordTextField3 setText:string];
    if(string.length == 1) {
    [wordTextField4 becomeFirstResponder];
    }
    }
      else if(theTextField == wordTextField4) {
      [wordTextField4 setText:string];
      if(string.length == 1) {
      [wordTextField1 becomeFirstResponder];
     }
     }
    }