Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind UISwitch to UIButton enable in RxSwift?

I have a UISwitch that needs to be selected in order to continue onto the next form screen. So I would like to bind the UISwitch selected to the UIButton enabled. I just can't get this sample going.

Here's what I'm trying but doesn't compile:

let termsValidation = termsSwitch
    .rx_selected
    .shareReplay(1)

termsValidation
    .bindTo(signupButton.rx_enabled)
    .addDisposableTo(disposeBag)

What's the correct way to get this to work in RxSwift and RxCocoa?

like image 434
TruMan1 Avatar asked May 27 '16 10:05

TruMan1


2 Answers

You should use rx_value rather than rx_selected.

like image 150
mrahmiao Avatar answered Oct 22 '22 03:10

mrahmiao


    let termsValidation = termsSwitch
            .rx.value
            .shareReplay(1)

    termsValidation
        .bind(to: signupButton.rx.isEnabled)
        .addDisposableTo(disposeBag)
like image 5
Kashif Avatar answered Oct 22 '22 04:10

Kashif