How to filter button tap with button title in RxSwift?

958 Views Asked by At

Is there anyway we can filter button tap event based on button title like below..!!

button.rx.tap
 .filter { ($0.titleLabel.text.count)!> 0 }
2

There are 2 best solutions below

0
Daniel T. On

There may be a way to do it, but it isn't a correct thing to do. You programmatically changed the button title so the code that does that should be referenced here. Don't use your view as if it was a model.

For example:

func example(title: Observable<String>, button: UIButton, bag: DisposeBag) -> Observable<String> {
    title
        .bind(to: button.rx.title(for: .normal))
        .disposed(by: bag)

    return button.rx.tap
        .withLatestFrom(title)
        .filter { !$0.isEmpty }
}
0
Nitin On

Finally was able to do as below, not sure where its perfect or not but serve purpose though..!!!

 button.rx.tap
                  .map(return button.titleLabel?.text!.lowercased()!)
                        .asDriver(onErrorJustReturn: "")
                        .drive(answerLabel.rx.text)
                        .disposed(by: bag)