I am observing UISearchBar.rx.text attributes to perform some search related Action when User types some text. But at some time, I also would like to trigger this search Action programmatically. For instance at the creation of the view like in this example, where unfortunately the "Searching for [...]" text is not printed.
import UIKit
import RxSwift
import RxCocoa
class ViewController: UIViewController {
@IBOutlet weak var mySearchBar: UISearchBar!
let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
// Trigger search when text changes
mySearchBar.rx.text.subscribe(onNext: { (text)
print("Searching for \(text)...")
// do some search Action
}
.disposed(by: disposeBag)
// Programmatically trigger a search
mySearchBar.text = "Some text to search"
}
}
The problem is changing mySearchBar.text does not trigger a new rx.text Event. Is there some way to do so?
For instance, I know thanks to this post that with an UITextField, this is possible using the UITextField.sendActions(for: .ValueChanged) function. Is there some similar way to do so with UISearchBar?