How to check text field input at real time?

前端 未结 7 1648
Happy的楠姐
Happy的楠姐 2020-12-09 21:07

I am writing validation for my textfield, I found something interesting that whether I can check how many digits I am typing into the textfield at real time. My text field i

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 21:28

    Update Swift 4.0

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

    Now call your function. In my case, this will enable the save button

    @objc func textFieldDidChange(_ textField: UITextField) {
        if (txtFieldAdd.text?.isEmpty)! {
            btnSave.isEnabled = false
        }
        else {
            btnSave.isEnabled = true
        }
    }
    

    Hope it helps.

提交回复
热议问题