我制作了如下Bool类型的Observable
let allValid: Observable<Bool>
//All valid is combination of two more Observable<Bool>
allValid = Observable.combineLatest(checkBoxValid, reasonValid) { $0 && $1 }
现在我想检查何时按下完成按钮,根据AllValid的值调用相应的方法。
public func doneButtonPressed() {
//Here I have two methods, and to be called, when AllValid is true and false
//self.method1()
//self.method2()
}
现在该怎么做。我无法直接绑定,因为它会触发,我想在按下Done时触发。
Rx的方法是把它放在你的viewDidLoad
中
let isValid = doneButton.rx.tap.withLatestFrom(allValid)
isValid
.filter { $0 }
.subscribe(onNext: { _ in
// The button was tapped while the last value from allValid was true.
}
.disposed(by: bag)
isValid
.filter { !$0 }
.subscribe(onNext: { _ in
// The button was tapped while the last value from allValid was false.
}
.disposed(by: bag)