可观察 如果是在RxSwift中

问题描述 投票:0回答:1

我制作了如下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时触发。

swift observable rx-swift
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.