Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField with "multiple" delegates

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]
  • Problem no. 1: on ios5 device the app crashes as soon as you tap on the text field that has a delegate set to self
  • Problem no. 2: i still need some text fields to be able to send delegate notifications to other objects.

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

like image 522
Marius Avatar asked Jan 23 '14 11:01

Marius


1 Answers

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)
}
like image 137
Paulo Mattos Avatar answered Nov 09 '22 04:11

Paulo Mattos