我观察到的布尔类型如下

let allValid: Observable<Bool>

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

现在我想检查一下当按下Done按钮时,根据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 { $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)

关于swift - Observable <Bool>如果在RxSwift中为其他,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52162086/

10-11 12:55