Observing UITextField.editing with RxSwift

前端 未结 3 827
别那么骄傲
别那么骄傲 2021-02-04 00:13

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

self.money.rx_observe(Bool.self, \"editing\").subscribeNext { (value) in
          


        
相关标签:
3条回答
  • 2021-02-04 00:53

    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)
    
    0 讨论(0)
  • 2021-02-04 00:55

    For RXSwift 3.0

    textField.rx.controlEvent([.editingDidBegin,.editingDidEnd])
            .asObservable()
            .subscribe(onNext: {
                print("editing state changed")
            }).disposed(by: disposeBag)
    
    0 讨论(0)
  • 2021-02-04 00:55

    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)
    
    0 讨论(0)
提交回复
热议问题