Observable <bool>如果在RxSwift中为其他

时间:2018-09-04 08:33:42

标签: swift observable rx-swift

我使如下所示的布尔类型为可观察

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()
}

现在如何制作它。我无法直接绑定,因为它将触发,并且我想在按下“完成”时触发。

1 个答案:

答案 0 :(得分:2)

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)