RxCocoa provide subscribe for UITextField and UITextView text change:
UITextField().rx.text.orEmpty.changed
.subscribe { text in
print(text)
}
.disposed(by: disposeBag)
UITextView().rx.text.changed.subscribe { t in
print(t)
}.disposed(by: disposeBag)
But no convenience for UILabel:
UILabel().rx.text.subscribe ...
Why rxcocoa designed for this?
In essence, the
textmethod is a wrapper around the UIControl'saddTarget(:action:for:)method. It turns that method into something that is Observable.The
UILabeltype isn't derived fromUIControland so doesn't have anaddTarget(:action:for:)method. You can't attach an@IBActionto a UILabel in normal UIKit, so you can't attach an Rx ControlProperty to a UILabel either.And just like in UIKit, you don't need to observe changes to a UILabel because the only way it changes is if your code changed it. Just observe the thing that triggered the label to change instead.
It will help to understand if you look at the code... The
textmethod is just a rename of thevaluemethod:and the
valuemethod is a ControlProperty that monitors the "default events".Looking at
controlPropertyWithDefaultEvents, we can find out what the default events are: