I'm trying to create a text field that would be able to respond to the following:
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
To do so i have a UITextField subclass which is a delegate for itself:
[self setDelegate:self]
QUESTION: What would be the easiest way to implement delegate methods in a subclass, but still allowing an outside object to be a delegate as well and recieve the same messages?
Thanks
I was having the exact same issue (in Swift 3). I solved the problem overriding the delegate
property from the UITextField
class.
During my custom view initialization, I hook up my own internal delegate:
class CustomTextField: UITextField, UITextFieldDelegate {
override public init(frame: CGRect) {
super.init(frame: frame)
initCustomTextField()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initCustomTextField()
}
private func initCustomTextField() {
super.delegate = self // Note the super qualifier.
}
...
Now we need to override the aforementioned delegate
property:
private weak var userDelegate: UITextFieldDelegate?
override var delegate: UITextFieldDelegate? {
get { return userDelegate }
set { userDelegate = newValue }
}
Finally, on each UITextFieldDelegate
protocol method you must forward the call to the external delegate, if any:
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
// Do your thing here, and then forward:
return self.delegate?.textFieldShouldBeginEditing?(self) ?? true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
// Do your thing here, and then forward:
self.delegate?.textFieldDidBeginEditing?(self)
}
...
One caveat if you plan to support iOS 10 as well:
func textFieldDidEndEditing(_ textField: UITextField) {
self.delegate?.textFieldDidEndEditing?(self)
}
/// This method will be called, instead of the above, on iOS ≥ 10.
@available(iOS 10.0, *)
func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason) {
self.delegate?.textFieldDidEndEditing?(self, reason: reason)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With