I am fairly new to IOS development and working on a small project, I am trying to subscribe to UITextView using Rxswift. However, I can't seem to find any way to do so online or by following what I did to UITextField.
this is what I Done and its working for TextField:
TextField.rx.controlEvent(.editingChanged).withLatestFrom(textField.rx.text.orEmpty)subscribe(onNext : { text in
self.viewModel.address.accept(text)
Some code goes here
Now doing the same thing for a textView
TextView.rx.controlEvent(.editingChanged).withLatestFrom(TextView.rx.text.orEmpty)subscribe(onNext : { text in
self.viewModel.address.accept(text)
I get the following error: Referencing instance method 'controlEvent' on 'Reactive' requires that 'UITextView' inherit from 'UIControl'
I am really confused on how to make controlEvent work with UITextField. it doesn't show in the suggested code when I use rx.
It's much easier than what you have in your question...
func example(textView: UITextView, textField: UITextField, disposeBag: DisposeBag) {
textView.rx.text
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
textField.rx.text
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
And the subscribe you have can be much simpler as well:
func example(textView: UITextView, observer: PublishRelay<String>, disposeBag: DisposeBag) {
textView.rx.text
.orEmpty
.bind(to: observer)
.disposed(by: disposeBag)
}
However, keep in mind:
Subjects [and Relays] provide a convenient way to poke around Rx, however they are not recommended for day to day use. An explanation is in the Usage Guidelines in the appendix. Instead of using subjects, favor the factory methods we will look at in Part 2. -- Intro to Rx
Break yourself of the habit of binding everything to a Relay or Subject now and you will find your future in Rx much easier.