简体   繁体   中英

Observing UITextField.editing with RxSwift

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

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)

For RXSwift 3.0

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

Since RxSwift 4.0, there is two specific control events : textDidBeginEditing and textDidEndEditing

You can used it like this :

textField.rx.textDidEndEditing
            .asObservable()
            .subscribe(onNext: {
                print("End of edition")
            }).disposed(by: disposeBag)


textField.rx.textDidBeginEditing
                .asObservable()
                .subscribe(onNext: {
                    print("Start of edition")
                }).disposed(by: disposeBag)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM