【问题标题】:Switching to next UITextField when character limit is reached达到字符限制时切换到下一个 UITextField
【发布时间】:2017-02-04 02:20:08
【问题描述】:

我需要在三个文本字段中输入电话号码。我正在尝试将每个文本字段的字符限制设置为三个字符,一旦达到此限制,就切换到新的文本字段。

我在网上看到用这个代码来限制字符:

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 个字符,然后切换到下一个字段。

【问题讨论】:

    标签: swift string uitextfield


    【解决方案1】:

    这是我的代码,我如何解决这个问题:

    三个文本字段是:

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

    UITextFieldDelegate 导入您的班级并将所有文本字段的委托设置为self。

    并使用此方法改变焦点。

    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。

    【讨论】:

      【解决方案2】:

      您可以使用UITextField shouldChangeCharactersInRange 的委托方法。不过,您必须做一些设置工作。这是一个创建 3 个文本字段的示例,符合 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
      }
      
      }
      

      【讨论】:

        【解决方案3】:

        我发现 Pierce 的答案太复杂了,anas.p 的答案对我来说并不像预期的那样有效。我的 Swift 5 解决方案:

        override func viewDidLoad() {
            textField1.addTarget(self, action: #selector(textFieldDidChange1), for:.editingChanged)
            textField2.addTarget(self, action: #selector(textFieldDidChange2), for:.editingChanged)
        }
        
        @objc func textFieldDidChange1() {
                // After 2 characters are typed, immediately jump to textField2
                if (textField1.text?.count == 2) {
                    self.textField2.becomeFirstResponder()
                }
            }
        
        @objc func textFieldDidChange2() {
            if (textField2.text?.count == 2) {
                self.textField3.becomeFirstResponder()
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-08
          • 2010-10-23
          • 1970-01-01
          • 2016-05-29
          • 2019-10-26
          • 1970-01-01
          相关资源
          最近更新 更多