Detecting a change to text in UITextfield

前端 未结 7 2007
轮回少年
轮回少年 2021-02-07 02:15

I would like to be able to detect if some text is changed in a UITextField so that I can then enable a UIButton to save the changes.

7条回答
  •  遥遥无期
    2021-02-07 02:55

    Swift 3.0

    Process 1

    Create IBOutlet of UITextfiled and Add Target to text field.

     m_lblTxt.addTarget(self, action: #selector(self.textFieldDidChange), for: UIControlEvents.editingChanged)
    
     func textFieldDidChange(textField:UITextField)
     {
         NSLog(textField.text!)
     }
    

    Process 2

     m_lblTxt.delegate = self
    
     //MARK: - TextField Delegates
     func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
     {
          print(textField.text!)
          return true
     }
    

提交回复
热议问题