26

While exploring RxSwift

I found just textField.rx_text.asObservable() property, that fires event every keyboard button is hit.

But how do we handle "search" or "done" button press events? I'd like to fire search only after these actions, not "search as you type".

alex
  • 2,121
  • 1
  • 18
  • 24

3 Answers3

46

You can subscribe to UIControlEvents like this:

textField.rx_controlEvents(.EditingDidEndOnExit).subscribeNext { print("return pressed") }

louoso
  • 795
  • 7
  • 9
23

In RxSwift 4 and 5:

textField.rx.controlEvent([.editingDidEndOnExit]).subscribe { _ in
    print("editingDidEndOnExit")
}.disposed(by: disposeBag)
d.felber
  • 5,288
  • 1
  • 21
  • 36
  • 1
    No need to add brackets around `.editingDidEndOnExit` – Borzh Oct 25 '19 at 22:07
  • What `print(text)` suppose to print, initially I thought it would be the text from the text field but when I tried it prints out empty bracket () – koira Sep 10 '20 at 12:57
  • @koira good point. "text" should have been named "event". but it does not contain valuable information so I removed it to prohibit confusion. – d.felber Sep 10 '20 at 18:48
6

In Rxswift 3.0

textField.rx.controlEvent([.editingDidEndOnExit])
         .subscribe(onNext:{text in
            print(text)
         }).addDisposableTo(disposeBag)
Chirag Desai
  • 827
  • 8
  • 13