Observable<Bool> If else 在 RxSwift 中

Observable<Bool> If else in RxSwift

我将 Observable 设为 Bool 类型,如下所示

let allValid: Observable<Bool>

//All valid is combination of two more Observable<Bool>
allValid = Observable.combineLatest(checkBoxValid, reasonValid) { [=11=] &&  }

现在我想检查“完成”按钮何时被按下,根据 AllValid 的值调用相应的方法。

public func doneButtonPressed() {
//Here I have two methods, and to be called, when AllValid is true and false

//self.method1()
//self.method2()
}

现在怎么做。我不能直接绑定,因为它会触发,我想在按下完成时触发。

执行此操作的 Rx 方法是将其放入您的 viewDidLoad

let isValid = doneButton.rx.tap.withLatestFrom(allValid)

isValid
    .filter { [=10=] }
    .subscribe(onNext: { _ in 
        // The button was tapped while the last value from allValid was true.
    }
    .disposed(by: bag)

isValid
    .filter { ![=10=] }
    .subscribe(onNext: { _ in 
        // The button was tapped while the last value from allValid was false.
    }
    .disposed(by: bag)