将光标移动到下一个UITextField而不在iOS中单击下一步

时间:2015-07-24 05:16:59

标签: ios objective-c iphone swift

  

如何在输入10位数后自动将控制移动到下一个文本字段。

3 个答案:

答案 0 :(得分:4)

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *currentString = [textField.text stringByReplacingCharactersInRange:range withString:string];
int length = [currentString length];
if (length > 10) {
   [yourNextTextfield becomeFirstResponder];
     return NO; // add the line 
}
return YES;
}

选择no-2

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
  NSUInteger newLength = [textField.text length] + [string length] - range.length;
 if (newLength >10)
    {
        [yournextTextfield becomeFirstResponder];
        return NO;
    }
 return YES;
}

<强>夫特

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
var currentString: String = textField.text!.stringByReplacingCharactersInRange(range, withString: string)
var length: Int = currentString.characters.count
if length > 10 {
    yourNextTextfield.becomeFirstResponder()
    return false
    // add the line
}
return true
}

另一种选择

   func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
var newLength: Int = textField.text!.characters.count + string.characters.count - range.length
if newLength > 10 {
    yournextTextfield.becomeFirstResponder()
    return false
}
return true
}

答案 1 :(得分:2)

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

     NSInteger nextTag = textField.tag + 1;

     // Try to find next responder
     UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];

     if (nextResponder) {

     // Found next responder, so set it.
     [nextResponder becomeFirstResponder];

     } else {

     // Not found, so remove keyboard.
     [textField resignFirstResponder];
     }
  return NO; // We do not want UITextField to insert line-breaks.
}

如果您有多个文本字段,也可以使用此

答案 2 :(得分:0)

-(BOOL) textFieldShouldReturn:(UITextField*) textField 
{
    if (textField == txt1){
    [txt1 resignFirstResponder];
    [txt2 becomeFirstResponder];
   }
   if (textField == txt2){
    [txt2 resignFirstResponder];
   }
    return YES;
}

不要忘记将代理 UITextFieldDelegate 添加到您的UITextfield