开发者

Descendant as an parent's delegate

开发者 https://www.devze.com 2023-04-01 08:49 出处:网络
Lets say I want to create my own text view with maximum characters constrain. And I want to do that constrain in level below - in text view.

Lets say I want to create my own text view with maximum characters constrain. And I want to do that constrain in level below - in text view.

I think of creating CustomTextView : UITextView where customTextView.delegate would be the same object - customTextView (self.delegate = self开发者_运维问答). The definition of the class would be CustomTextView : UITextView <UITextVIewDelegate> and I would implement – textView:shouldChangeTextInRange:replacementText: to do the constrain logic in.

But somehow this do not work. Can I get explanation why or what can be wrong and how to achieve my intent?


If you are subclassing UITextView, why would you need to set itself as the delegate? The delegate is only used to notify code outside of the UITextView that something changed in the UITextView. This means that the UITextView is notified of changes to itself first and, using the delegate, you can notify external code (UIViewController, etc.) of what happened. If you are subclassing the UITextView, it should receive those change notifications from the OS.

However, looking through the documentation, I cannot see how you would track the built-in events by subclass alone. Here's an article I found with a Google search: Subclassing a UITextView

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号