How do I check when a UITextField changes?

后端 未结 19 2960
情话喂你
情话喂你 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:25

    textField(_:shouldChangeCharactersIn:replacementString:) worked for me in Xcode 8, Swift 3 if you want to check every single keypress.

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
        // Whatever code you want to run here.
        // Keep in mind that the textfield hasn't yet been updated,
        // so use 'string' instead of 'textField.text' if you want to
        // access the string the textfield will have after a user presses a key
    
        var statusText = self.status.text
        var usernameText = self.username.text
    
        switch textField{
        case self.status:
            statusText = string
        case self.username:
            usernameText = string
        default:
            break
        }
    
        if statusText == "" && usernameText == "" {
            self.topRightButton.enabled = false
        } else {   
            self.topRightButton.enabled = true
        }
    
        //Return false if you don't want the textfield to be updated
        return true
    }
    
    0 讨论(0)
  • 2020-11-22 16:25
    txf_Subject.addTarget(self, action:#selector(didChangeFirstText), for: .editingChanged)
    
    @objc func didChangeText(textField:UITextField) {
        let str = textField.text
        if(str?.contains(" "))!{
            let newstr = str?.replacingOccurrences(of: " ", with: "")
            textField.text = newstr
        }
    }
    
    @objc func didChangeFirstText(textField:UITextField) {
        if(textField.text == " "){
            textField.text = ""
        }
    }
    
    0 讨论(0)
  • 2020-11-22 16:25

    swift 4

    In viewDidLoad():

        //ADD BUTTON TO DISMISS KEYBOARD
    
        // Init a keyboard toolbar 
        let toolbar = UIView(frame: CGRect(x: 0, y: view.frame.size.height+44, width: view.frame.size.width, height: 44))
        toolbar.backgroundColor = UIColor.clear
    
        // Add done button
        let doneButt = UIButton(frame: CGRect(x: toolbar.frame.size.width - 60, y: 0, width: 44, height: 44))
        doneButt.setTitle("Done", for: .normal)
        doneButt.setTitleColor(MAIN_COLOR, for: .normal)
        doneButt.titleLabel?.font = UIFont(name: "Titillium-Semibold", size: 13)
        doneButt.addTarget(self, action: #selector(dismissKeyboard), for: .touchUpInside)
        toolbar.addSubview(doneButt)
    
        USDTextField.inputAccessoryView = toolbar
    

    Add this function:

        @objc func dismissKeyboard() {
          //Causes the view (or one of its embedded text fields) to resign the first responder status.
          view.endEditing(true)
        }
    
    0 讨论(0)
  • This is how you can add a textField text change listener using Swift 3:

    Declare your class as UITextFieldDelegate

    override func viewDidLoad() {
        super.viewDidLoad()
    
        textField.delegate = self
    
        textField.addTarget(self, action: #selector(UITextFieldDelegate.textFieldShouldEndEditing(_:)), for: UIControlEvents.editingChanged)
    }
    

    Then just traditionally add a textFieldShouldEndEditing function:

    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool { // do stuff
            return true 
    }
    
    0 讨论(0)
  • 2020-11-22 16:28

    Swift 5.0

    textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)),
                              for: .editingChanged)
    

    and handle method:

    @objc func textFieldDidChange(_ textField: UITextField) {
    
    }
    

    Swift 4.0

    textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)),
                              for: UIControlEvents.editingChanged)
    

    and handle method:

    @objc func textFieldDidChange(_ textField: UITextField) {
    
    }
    

    Swift 3.0

    textField.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
    

    and handle method:

    func textFieldDidChange(textField: UITextField) { 
    
    }
    
    0 讨论(0)
  • 2020-11-22 16:30

    You can use this delegate method from UITextFieldDelegate. It fires with every character change.

    (Objective C) textField:shouldChangeCharactersInRange:replacementString:
    (Swift) textField(_:shouldChangeCharactersInRange:replacementString:)
    

    However THIS ONLY FIRES BEFORE a change is made (indeed, a change is only made if you do return true from here).

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