Observing UITextField.editing with RxSwift

C.liu picture C.liu · Sep 22, 2016 · Viewed 22k times · Source

I want to observe the property UITextfield.editing. I'm using this code:

self.money.rx_observe(Bool.self, "editing").subscribeNext { (value) in
    print("")
}.addDisposableTo(disposeBag)

But in the process of running, it's only performed once. How do I solve this,please

Answer

solidcell picture solidcell · Sep 22, 2016

Don't observe the editing property, because it's not just a stored property. It's defined as:

public var editing: Bool { get }

So you don't know how UIKit is actually getting that value.

Instead, use rx.controlEvent and specify the control events you're interested in, like so:

textField.rx.controlEvent([.editingDidBegin, .editingDidEnd])
    .asObservable()
    .subscribe(onNext: { _ in
        print("editing state changed")
    })
    .disposed(by: disposeBag)