2017-02-04 27 views
-1

我有三个文本字段可输入电话号码。我试图将每个文本字段的字符限制设置为三个字符,并且一旦达到此字符切换到新的文本字段。达到字符数限制时切换到下一个UITextField

我在网上看到使用此代码来限制字符:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 

    let currentCharacterCount = textField.text?.characters.count ?? 0 
    if (range.length + range.location > currentCharacterCount){ 
     return false 
    } 
    let newLength = currentCharacterCount + string.characters.count - range.length 
    return newLength <= 25 
} 

,并用它来切换到输入一个新的文本框:

.didbecomefirstresponder() 

,但我不知道如何将文本字段限制为3个字符,然后切换到下一个字段。

回答

1

您可以使用代理方法UITextFieldshouldChangeCharactersInRange。尽管如此,你必须做一些设置工作。以下是一个创建3个textField的示例,符合UITextFieldDelegate,并执行类似于您所描述的操作。

class ViewController: UIViewController, UITextFieldDelegate { 


var max = 3 
var fields:[UITextField] = [] 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 


    // This is just an example to layout 3 text fields. 
    for i in 0 ..< 3 { 
     let field = UITextField() 
     view.addSubview(field) 
     field.delegate = self 
     field.borderStyle = .roundedRect 
     field.font = UIFont(name: "AvenirNext-Regular", size: 15.0) 
     field.textColor = UIColor.black 
     field.frame = CGRect(x: view.bounds.width/2 - 100, y: 100 + (150*CGFloat(i)), width: 200, height: 50) 
     fields.append(field) 
    } 


} 

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 
    // Make sure the field has non-nil text 
    if let text = textField.text { 
     // Check if the text length has reached max limit 
     if text.characters.count > (max-1) { 
      // Get index of field from fields array 
      if let index = fields.index(of: textField) { 
       // Make sure it's not the last field in the array else return false 
       if index < fields.count-1 { 
        // Make the next field in the array become first responder if it's text is non-nil and it's text's character count is less than desired max 
        if fields[index+1].text != nil && fields[index+1].text!.characters.count < (max-1) { 
         fields[index+1].becomeFirstResponder() 
        } else { 
         return false 
        } 
       } else { 
        return false 
       } 
      } 
     } 
    } 
    return true 
} 

} 
2

这是我的代码,我该怎么解决这个问题:

三个文本框是:

@IBOutlet var txtField1: UITextField! 
@IBOutlet var txtField2: UITextField! 
@IBOutlet var txtField3: UITextField! 

进口UITextFieldDelegate到您的类,并设置委托给自己的所有文本框。

并使用此方法更改焦点。

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 

    let currentCharacterCount = ((textField.text?.characters.count)! + string.characters.count) - 1 

    switch (textField, currentCharacterCount) { 
    case (self.txtField1, 3): 
     self.txtField2.becomeFirstResponder() 
    case (self.txtField2, 7): 
     self.txtField3.becomeFirstResponder() 
    default: 
     break 
    } 

    return true 
} 

这里我为第一个文本字段设置了字符数3,第二个文本字段设置了7。

相关问题