Observing UITextField.editing with RxSwift

主宰稳场 提交于 2019-12-31 18:58:31

问题


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


回答1:


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)



回答2:


For RXSwift 3.0

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



回答3:


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)


来源:https://stackoverflow.com/questions/39627440/observing-uitextfield-editing-with-rxswift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!