简体   繁体   中英

RxSwift Text field Delegate Methods

I'm new to RxSwift. I need to ask how can I bind textField delegate methods using RxSwift like textFieldShouldReturn or textViewDidBeginEditing.

This will bind textfield delegate methods

textfield.rx.controlEvent([.EditingDidBegin]).asObservable().subscribe(on:{_ in 
    print("edit begin")
 }).addDisposableTo(disposer)

RxSwift, RxSwiftExt, RxCocoa 5.0:

let textField = UITextField()
textField.rx.controlEvent(.editingDidBegin).subscribe(onNext: { () in

}, onError: { (error) in

}, onCompleted: {

}).disposed(by: disposeBag)

this is one way of getting the text only when editing is done

cell.nameLabel.rx.controlEvent(.editingDidEnd).map{return
                        cell.nameLabel.text!
                    }

Let's supose you have a viewModel . For instance:

class ViewModel {
    var name: Variable<String>
    // more properties ...
}

Then, in your viewController :

textField.rx.text.orEmpty
    .bindTo(viewModel.name)
    .addDisposableTo(disposeBag)

In your viewController you have to import RxSwift and RxCocoa

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