How would I update a table view row as as a text field text changes? It is difficult to create a string from the arguments in textField(:shouldChangeCharactersIn:replacementString:) so that the string I create would be equal to the string value of the text property of the text field after the textField(:shouldChangeCharactersIn:replacementString:) occurs. Is there a way to use the UITextInput protocol of UITextField? I haven't found anything on stackoverflow about using the UITextInput protocol of UITextField except a question in 2012 using Objective-C that asks why he's getting a crash.
CodePudding user response:
If I understand your question correctly, you want something that calls a function every time the user edits the text field. You can do this by either connecting a function via the storyboard, or use a target in viewDidLoad()
Here's how I usually use it in viewDidLoad()
:
textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
textField.tag = 0 //Use this to differentiate between text fields if needed
And in my textFieldDidChange function:
@objc func textFieldDidChange(_ textField: UITextField) {
String text = textField.text
//Do something else
}
I hope I understood your question correctly. You can also connect an action from the storyboard for editingChanged
, similar to how you would do it for a UIButton.