I want to check when the user starts editing a text field. There is a great clear answer on how to do that here.
However, in my case my textField is within a UITableview that's set up as its own class. I've tried lots of different ways to get this to work, but I keep getting the crash "libc++abi.dylib: terminating with uncaught exception of type NSException" I put a break in the textFieldDidChange func and it never gets called so the problem seems to be with how I'm calling that func from the target.
class TextFieldCell: UITableViewCell {
lazy var textField: UITextField = {
let tf = UITextField()
tf.translatesAutoresizingMaskIntoConstraints = false
tf.textAlignment = .center
tf.textColor = .black
tf.font = UIFont.systemFont(ofSize: 17)
tf.clearButtonMode = .whileEditing
return tf
}()
// For simplicity, the rest of the Cell setup not shown.
// Adds target in AirInput VC to fire method when editing happens
textField.addTarget(self, action: #selector(AirInputViewController.textFieldDidChange(_:)), for: UIControl.Event.editingChanged)
}
class AirInputViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {
@objc func textFieldDidChange(_ textField: UITextField) {
}
}
I also tried the following for the target and it crashes as well.
textField.addTarget(AirInputViewController.self, action: #selector(AirInputViewController.textFieldDidChange(_:)), for: UIControl.Event.editingChanged)
It feels like I'm missing something simple, but I have no idea what that simple fix is. Or perhaps should I add the target in the AirInputViewContoller? If so, how would I access the UITableViewCells where the Text Field is? Thanks!