Let's say I add a UITextView
to my UIView
, and I want it to change its background color each time the contents change. I can do this by becoming the delegate of the UITextView
and implementing textViewDidChange
.
If I am using this behavior frequently though, it makes sense to create a UITextView
subclass, which I will call ColorSwitchingTextView
. It should include the color-switching behavior by default, so that any UIView
can simply add it instead of a standard UITextView
if it wants that behavior.
How do I detect changes in the content from within my ColorSwitchingTextView
class? I don't think I can do something like self.delegate = self
.
In summary, how can a UITextView
subclass know when its contents change?
EDIT
It seems I can use self.delegate = self
, but this means that the UIViewController that uses the ColorSwitchingTextView
can not also subscribe to the notifications. Once I use switchingTextView.delegate = self
in the view controller, the subclass behavior no longer works. Any workarounds? I'm trying to get a custom UITextView
that otherwise works like a regular UITextView
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…