Delegate Methods
extension ViewController: UITextFieldDelegate {
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let word = textField.text ?? ""
let char = string
print("Default - shouldChangeCharactersIn: \(word) \(char)")
return true
}
func textFieldShouldClear(_ textField: UITextField) -> Bool {
return true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.endEditing(true)
return true
}
}
텍스트 변경 알림 받기
textField.addTarget(self, action: #selector(textFieldEditingChanged), for: .editingChanged)
@objc func textFieldEditingChanged(_ sender: UITextField) {
print(sender.text!)
}