简体   繁体   中英

Reusing implementation of UITextField Delegate methods

In my application I want to use a UITextField multiple times. And for each of this UITextField I want to implement the UITextFieldDelegate methods (didEndEditing, didBeginEditing, should...).

My idea was to subclass the UITextField and set the delegate to itself. Then implementing the delegate methods as I want them to be implemented. It this case I would be able to use the subclassed UITextField anywhere, and the delegate methods will be implemented once, so I won't have to implement them in every VC separately.

BUT!

Delegation is used the communicate to another object to delegate some stuff to that object. It makes no sense to set the delegate to itself.

How should I implement the delegate methods ONCE so every time I place the UITextField using Interface Builder and set the class to the custom UITextField, the textfield will behave according tot the implemented delegate methods.

Thanks for all your help

My solution might not be optimal and I'm still seeking for a better one, but has no one is answering you, here we go :

A UIView should never try to "control" anything : it's just a view. That's why through my projects, all my UIViewController inherit from a base UIViewController ( BaseViewController ). This BaseViewController has all the UITextField delegation code, so whenever a UIViewController set a UITextField delegate to self , the BaseViewController handles it. Because I want my BaseViewController to be able to control all my view's UIScrollView content offset, I also have a base class BaseView which all my UIViews are inheriting, that is having by default a UIScrollview .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM