How do I check when a UITextField changes?

后端 未结 19 2959
情话喂你
情话喂你 2020-11-22 15:44

I am trying to check when a text field changes, equivalent too the function used for textView - textViewDidChange so far I have done this:

  fu         


        
相关标签:
19条回答
  • 2020-11-22 16:17

    Swift 4

    Conform to UITextFieldDelegate.

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        // figure out what the new string will be after the pending edit
        let updatedString = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)
    
        // Do whatever you want here
    
    
        // Return true so that the change happens
        return true
    }
    
    0 讨论(0)
  • 2020-11-22 16:17

    There's now a UITextField delegate method available on iOS13+

    optional func textFieldDidChangeSelection(_ textField: UITextField)
    
    0 讨论(0)
  • 2020-11-22 16:17

    Just in case you are interested in a SwiftUI solution, this it's working for me:

     TextField("write your answer here...",
                text: Binding(
                         get: {
                            return self.query
                           },
                         set: { (newValue) in
                            self.fetch(query: newValue) // any action you need
                                    return self.query = newValue
                          }
                )
      )
    

    I have to say it's not my idea, I read it in this blog: SwiftUI binding: A very simple trick

    0 讨论(0)
  • 2020-11-22 16:18

    Swift 3.0.1+ (Some of the other swift 3.0 answers are not up to date)

    textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)),
                              for: UIControlEvents.editingChanged)
    
    func textFieldDidChange(_ textField: UITextField) {
    
    }
    
    0 讨论(0)
  • 2020-11-22 16:20

    In case it is not possible to bind the addTarget to your UITextField, I advise you to bind one of them as suggested above, and insert the code for execution at the end of the shouldChangeCharactersIn method.

    nameTextField.addTarget(self, action: #selector(RegistrationViewController.textFieldDidChange(_:)), for: .editingChanged)
    
    @objc func textFieldDidChange(_ textField: UITextField) {
        if phoneNumberTextField.text!.count == 17 && nameTextField.text!.count > 0 {
            continueButtonOutlet.backgroundColor = UIColor(.green)
        } else {
            continueButtonOutlet.backgroundColor = .systemGray
        }
    }
    

    And in call in shouldChangeCharactersIn func.

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
        guard let text = textField.text else {
            return true
        }
        let lastText = (text as NSString).replacingCharacters(in: range, with: string) as String
    
        if phoneNumberTextField == textField {
            textField.text = lastText.format("+7(NNN)-NNN-NN-NN", oldString: text)
            textFieldDidChange(phoneNumberTextField)
            return false
        }
        return true
    }
    
    0 讨论(0)
  • 2020-11-22 16:24

    You can make this connection in interface builder.

    1. In your storyboard, click the assistant editor at the top of the screen (two circles in the middle).

    2. Ctrl + Click on the textfield in interface builder.

    3. Drag from EditingChanged to inside your view controller class in the assistant view.

    4. Name your function ("textDidChange" for example) and click connect.

    0 讨论(0)
提交回复
热议问题