( Swift 3). delegate
UITextField
.
:
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
}
...
delegate
:
private weak var userDelegate: UITextFieldDelegate?
override var delegate: UITextFieldDelegate? {
get { return userDelegate }
set { userDelegate = newValue }
}
, UITextFieldDelegate
, :
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return self.delegate?.textFieldShouldBeginEditing?(self) ?? true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
self.delegate?.textFieldDidEndEditing?(self)
}
...
, iOS 10:
func textFieldDidEndEditing(_ textField: UITextField) {
self.delegate?.textFieldDidEndEditing?(self)
}
@available(iOS 10.0, *)
func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason) {
self.delegate?.textFieldDidEndEditing?(self, reason: reason)
}