【问题标题】:Observable<Bool> If else in RxSwiftObservable<Bool> If else 在 RxSwift
【发布时间】:2018-09-04 08:33:42
【问题描述】:

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

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 时触发。

【问题讨论】:

  • doneButtonPressed 应该由按钮事件触发。

标签: swift observable rx-swift


【解决方案1】:

执行此操作的 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多