How to subscribe on the value changed control event of a UISwitch Using Rxswift











up vote
1
down vote

favorite












I want to use Rxswift and not IBActions to solve my issue below,



I have a UISwitch and I want to subscribe to the value changed event in
it,



I usually subscribe on Buttons using this manner



@IBOutlet weak var myButton: UIButton!


myButton
.rx
.tapGesture()
.when(.recognized)
.subscribe(onNext : {_ in /*do action here */})


Does anyone know how to subscribe to UISwitch control events?










share|improve this question
























  • Does yourSwitch.rx.value.changed.subscribe(onNext: ...) work?
    – Sweeper
    Nov 19 at 19:20












  • I tried it now, and it didnt
    – MhmdRizk
    Nov 19 at 19:33















up vote
1
down vote

favorite












I want to use Rxswift and not IBActions to solve my issue below,



I have a UISwitch and I want to subscribe to the value changed event in
it,



I usually subscribe on Buttons using this manner



@IBOutlet weak var myButton: UIButton!


myButton
.rx
.tapGesture()
.when(.recognized)
.subscribe(onNext : {_ in /*do action here */})


Does anyone know how to subscribe to UISwitch control events?










share|improve this question
























  • Does yourSwitch.rx.value.changed.subscribe(onNext: ...) work?
    – Sweeper
    Nov 19 at 19:20












  • I tried it now, and it didnt
    – MhmdRizk
    Nov 19 at 19:33













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I want to use Rxswift and not IBActions to solve my issue below,



I have a UISwitch and I want to subscribe to the value changed event in
it,



I usually subscribe on Buttons using this manner



@IBOutlet weak var myButton: UIButton!


myButton
.rx
.tapGesture()
.when(.recognized)
.subscribe(onNext : {_ in /*do action here */})


Does anyone know how to subscribe to UISwitch control events?










share|improve this question















I want to use Rxswift and not IBActions to solve my issue below,



I have a UISwitch and I want to subscribe to the value changed event in
it,



I usually subscribe on Buttons using this manner



@IBOutlet weak var myButton: UIButton!


myButton
.rx
.tapGesture()
.when(.recognized)
.subscribe(onNext : {_ in /*do action here */})


Does anyone know how to subscribe to UISwitch control events?







ios swift rx-swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 20:51









Damon

500317




500317










asked Nov 19 at 19:16









MhmdRizk

426310




426310












  • Does yourSwitch.rx.value.changed.subscribe(onNext: ...) work?
    – Sweeper
    Nov 19 at 19:20












  • I tried it now, and it didnt
    – MhmdRizk
    Nov 19 at 19:33


















  • Does yourSwitch.rx.value.changed.subscribe(onNext: ...) work?
    – Sweeper
    Nov 19 at 19:20












  • I tried it now, and it didnt
    – MhmdRizk
    Nov 19 at 19:33
















Does yourSwitch.rx.value.changed.subscribe(onNext: ...) work?
– Sweeper
Nov 19 at 19:20






Does yourSwitch.rx.value.changed.subscribe(onNext: ...) work?
– Sweeper
Nov 19 at 19:20














I tried it now, and it didnt
– MhmdRizk
Nov 19 at 19:33




I tried it now, and it didnt
– MhmdRizk
Nov 19 at 19:33












3 Answers
3






active

oldest

votes

















up vote
1
down vote













Below are some caveats you would use for UISwitch:

1. Make sure the event subscribes to unique changes so use distinctUntilChanged
2. Rigorous switching the switch can cause unexpected behavior so use debounce.

Example:

anySwitch.rx
.isOn.changed //when state changed
.debounce(0.8, scheduler: MainScheduler.instance) //handle rigorous user switching
.distinctUntilChanged().asObservable() //take signal if state is different than before. This is optional depends on your use case
.subscribe(onNext:{[weak self] value in
//your code
}).disposed(by: disposeBag)





share|improve this answer






























    up vote
    1
    down vote



    accepted










    I found the answer Im looking for, in order to subscribe on and control event we should do the below :



    @IBOutlet weak var mySwitch : UISwitch!

    mySwitch
    .rx
    .controlEvent(.valueChanged)
    .withLatestFrom(mySwitch.rx.value)
    .subscribe(onNext : { bool in
    // this is the value of mySwitch
    })
    .disposed(by: disposeBag)





    share|improve this answer





















    • Looks good. Congrats. :)
      – Glenn
      Nov 20 at 9:11


















    up vote
    1
    down vote













    There are couple of ways to do that. But this one is how I usually do it:
    Try this out.



    self.mySwitch.rx.isOn.subscribe { isOn in
    print(isOn)
    }.disposed(by: self.disposeBag)


    I hope this helps.



    EDIT:



    Another would be subscribing to the value rx property of the UISwitch, like so:



    mySwitch.rx.value.subscribe { (isOn) in
    print(isOn)
    }.disposed(by: self.disposeBag)


    Now, as for your comment:




    this worked for me thanks , but I preferred subscribing on the control
    event it self, not the value.




    We could do this below, I'm not sure though if there's a better way than this. Since UISwitch is a UIControl object, you can subscribe to its .valueChanged event, like so:



        mySwitch.rx.controlEvent([.valueChanged]).subscribe { _ in
    print("isOn? : (mySwitch.isOn)")
    }.disposed(by: self.disposeBag)


    More info: https://developer.apple.com/documentation/uikit/uiswitch






    share|improve this answer























    • this worked for me thanks , but I preferred subscribing on the control event it self, not the value
      – MhmdRizk
      Nov 20 at 7:19










    • See my updated answer. :)
      – Glenn
      Nov 20 at 8:55










    • thanks for the effort bro, but I posted the solution and it worked by subscribe on the value changed event, check my answer
      – MhmdRizk
      Nov 20 at 9:03











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381212%2fhow-to-subscribe-on-the-value-changed-control-event-of-a-uiswitch-using-rxswift%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Below are some caveats you would use for UISwitch:

    1. Make sure the event subscribes to unique changes so use distinctUntilChanged
    2. Rigorous switching the switch can cause unexpected behavior so use debounce.

    Example:

    anySwitch.rx
    .isOn.changed //when state changed
    .debounce(0.8, scheduler: MainScheduler.instance) //handle rigorous user switching
    .distinctUntilChanged().asObservable() //take signal if state is different than before. This is optional depends on your use case
    .subscribe(onNext:{[weak self] value in
    //your code
    }).disposed(by: disposeBag)





    share|improve this answer



























      up vote
      1
      down vote













      Below are some caveats you would use for UISwitch:

      1. Make sure the event subscribes to unique changes so use distinctUntilChanged
      2. Rigorous switching the switch can cause unexpected behavior so use debounce.

      Example:

      anySwitch.rx
      .isOn.changed //when state changed
      .debounce(0.8, scheduler: MainScheduler.instance) //handle rigorous user switching
      .distinctUntilChanged().asObservable() //take signal if state is different than before. This is optional depends on your use case
      .subscribe(onNext:{[weak self] value in
      //your code
      }).disposed(by: disposeBag)





      share|improve this answer

























        up vote
        1
        down vote










        up vote
        1
        down vote









        Below are some caveats you would use for UISwitch:

        1. Make sure the event subscribes to unique changes so use distinctUntilChanged
        2. Rigorous switching the switch can cause unexpected behavior so use debounce.

        Example:

        anySwitch.rx
        .isOn.changed //when state changed
        .debounce(0.8, scheduler: MainScheduler.instance) //handle rigorous user switching
        .distinctUntilChanged().asObservable() //take signal if state is different than before. This is optional depends on your use case
        .subscribe(onNext:{[weak self] value in
        //your code
        }).disposed(by: disposeBag)





        share|improve this answer














        Below are some caveats you would use for UISwitch:

        1. Make sure the event subscribes to unique changes so use distinctUntilChanged
        2. Rigorous switching the switch can cause unexpected behavior so use debounce.

        Example:

        anySwitch.rx
        .isOn.changed //when state changed
        .debounce(0.8, scheduler: MainScheduler.instance) //handle rigorous user switching
        .distinctUntilChanged().asObservable() //take signal if state is different than before. This is optional depends on your use case
        .subscribe(onNext:{[weak self] value in
        //your code
        }).disposed(by: disposeBag)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 at 0:20

























        answered Nov 20 at 0:13









        prekshya basnet

        1368




        1368
























            up vote
            1
            down vote



            accepted










            I found the answer Im looking for, in order to subscribe on and control event we should do the below :



            @IBOutlet weak var mySwitch : UISwitch!

            mySwitch
            .rx
            .controlEvent(.valueChanged)
            .withLatestFrom(mySwitch.rx.value)
            .subscribe(onNext : { bool in
            // this is the value of mySwitch
            })
            .disposed(by: disposeBag)





            share|improve this answer





















            • Looks good. Congrats. :)
              – Glenn
              Nov 20 at 9:11















            up vote
            1
            down vote



            accepted










            I found the answer Im looking for, in order to subscribe on and control event we should do the below :



            @IBOutlet weak var mySwitch : UISwitch!

            mySwitch
            .rx
            .controlEvent(.valueChanged)
            .withLatestFrom(mySwitch.rx.value)
            .subscribe(onNext : { bool in
            // this is the value of mySwitch
            })
            .disposed(by: disposeBag)





            share|improve this answer





















            • Looks good. Congrats. :)
              – Glenn
              Nov 20 at 9:11













            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            I found the answer Im looking for, in order to subscribe on and control event we should do the below :



            @IBOutlet weak var mySwitch : UISwitch!

            mySwitch
            .rx
            .controlEvent(.valueChanged)
            .withLatestFrom(mySwitch.rx.value)
            .subscribe(onNext : { bool in
            // this is the value of mySwitch
            })
            .disposed(by: disposeBag)





            share|improve this answer












            I found the answer Im looking for, in order to subscribe on and control event we should do the below :



            @IBOutlet weak var mySwitch : UISwitch!

            mySwitch
            .rx
            .controlEvent(.valueChanged)
            .withLatestFrom(mySwitch.rx.value)
            .subscribe(onNext : { bool in
            // this is the value of mySwitch
            })
            .disposed(by: disposeBag)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 at 7:18









            MhmdRizk

            426310




            426310












            • Looks good. Congrats. :)
              – Glenn
              Nov 20 at 9:11


















            • Looks good. Congrats. :)
              – Glenn
              Nov 20 at 9:11
















            Looks good. Congrats. :)
            – Glenn
            Nov 20 at 9:11




            Looks good. Congrats. :)
            – Glenn
            Nov 20 at 9:11










            up vote
            1
            down vote













            There are couple of ways to do that. But this one is how I usually do it:
            Try this out.



            self.mySwitch.rx.isOn.subscribe { isOn in
            print(isOn)
            }.disposed(by: self.disposeBag)


            I hope this helps.



            EDIT:



            Another would be subscribing to the value rx property of the UISwitch, like so:



            mySwitch.rx.value.subscribe { (isOn) in
            print(isOn)
            }.disposed(by: self.disposeBag)


            Now, as for your comment:




            this worked for me thanks , but I preferred subscribing on the control
            event it self, not the value.




            We could do this below, I'm not sure though if there's a better way than this. Since UISwitch is a UIControl object, you can subscribe to its .valueChanged event, like so:



                mySwitch.rx.controlEvent([.valueChanged]).subscribe { _ in
            print("isOn? : (mySwitch.isOn)")
            }.disposed(by: self.disposeBag)


            More info: https://developer.apple.com/documentation/uikit/uiswitch






            share|improve this answer























            • this worked for me thanks , but I preferred subscribing on the control event it self, not the value
              – MhmdRizk
              Nov 20 at 7:19










            • See my updated answer. :)
              – Glenn
              Nov 20 at 8:55










            • thanks for the effort bro, but I posted the solution and it worked by subscribe on the value changed event, check my answer
              – MhmdRizk
              Nov 20 at 9:03















            up vote
            1
            down vote













            There are couple of ways to do that. But this one is how I usually do it:
            Try this out.



            self.mySwitch.rx.isOn.subscribe { isOn in
            print(isOn)
            }.disposed(by: self.disposeBag)


            I hope this helps.



            EDIT:



            Another would be subscribing to the value rx property of the UISwitch, like so:



            mySwitch.rx.value.subscribe { (isOn) in
            print(isOn)
            }.disposed(by: self.disposeBag)


            Now, as for your comment:




            this worked for me thanks , but I preferred subscribing on the control
            event it self, not the value.




            We could do this below, I'm not sure though if there's a better way than this. Since UISwitch is a UIControl object, you can subscribe to its .valueChanged event, like so:



                mySwitch.rx.controlEvent([.valueChanged]).subscribe { _ in
            print("isOn? : (mySwitch.isOn)")
            }.disposed(by: self.disposeBag)


            More info: https://developer.apple.com/documentation/uikit/uiswitch






            share|improve this answer























            • this worked for me thanks , but I preferred subscribing on the control event it self, not the value
              – MhmdRizk
              Nov 20 at 7:19










            • See my updated answer. :)
              – Glenn
              Nov 20 at 8:55










            • thanks for the effort bro, but I posted the solution and it worked by subscribe on the value changed event, check my answer
              – MhmdRizk
              Nov 20 at 9:03













            up vote
            1
            down vote










            up vote
            1
            down vote









            There are couple of ways to do that. But this one is how I usually do it:
            Try this out.



            self.mySwitch.rx.isOn.subscribe { isOn in
            print(isOn)
            }.disposed(by: self.disposeBag)


            I hope this helps.



            EDIT:



            Another would be subscribing to the value rx property of the UISwitch, like so:



            mySwitch.rx.value.subscribe { (isOn) in
            print(isOn)
            }.disposed(by: self.disposeBag)


            Now, as for your comment:




            this worked for me thanks , but I preferred subscribing on the control
            event it self, not the value.




            We could do this below, I'm not sure though if there's a better way than this. Since UISwitch is a UIControl object, you can subscribe to its .valueChanged event, like so:



                mySwitch.rx.controlEvent([.valueChanged]).subscribe { _ in
            print("isOn? : (mySwitch.isOn)")
            }.disposed(by: self.disposeBag)


            More info: https://developer.apple.com/documentation/uikit/uiswitch






            share|improve this answer














            There are couple of ways to do that. But this one is how I usually do it:
            Try this out.



            self.mySwitch.rx.isOn.subscribe { isOn in
            print(isOn)
            }.disposed(by: self.disposeBag)


            I hope this helps.



            EDIT:



            Another would be subscribing to the value rx property of the UISwitch, like so:



            mySwitch.rx.value.subscribe { (isOn) in
            print(isOn)
            }.disposed(by: self.disposeBag)


            Now, as for your comment:




            this worked for me thanks , but I preferred subscribing on the control
            event it self, not the value.




            We could do this below, I'm not sure though if there's a better way than this. Since UISwitch is a UIControl object, you can subscribe to its .valueChanged event, like so:



                mySwitch.rx.controlEvent([.valueChanged]).subscribe { _ in
            print("isOn? : (mySwitch.isOn)")
            }.disposed(by: self.disposeBag)


            More info: https://developer.apple.com/documentation/uikit/uiswitch







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 20 at 8:55

























            answered Nov 19 at 19:42









            Glenn

            4,55921740




            4,55921740












            • this worked for me thanks , but I preferred subscribing on the control event it self, not the value
              – MhmdRizk
              Nov 20 at 7:19










            • See my updated answer. :)
              – Glenn
              Nov 20 at 8:55










            • thanks for the effort bro, but I posted the solution and it worked by subscribe on the value changed event, check my answer
              – MhmdRizk
              Nov 20 at 9:03


















            • this worked for me thanks , but I preferred subscribing on the control event it self, not the value
              – MhmdRizk
              Nov 20 at 7:19










            • See my updated answer. :)
              – Glenn
              Nov 20 at 8:55










            • thanks for the effort bro, but I posted the solution and it worked by subscribe on the value changed event, check my answer
              – MhmdRizk
              Nov 20 at 9:03
















            this worked for me thanks , but I preferred subscribing on the control event it self, not the value
            – MhmdRizk
            Nov 20 at 7:19




            this worked for me thanks , but I preferred subscribing on the control event it self, not the value
            – MhmdRizk
            Nov 20 at 7:19












            See my updated answer. :)
            – Glenn
            Nov 20 at 8:55




            See my updated answer. :)
            – Glenn
            Nov 20 at 8:55












            thanks for the effort bro, but I posted the solution and it worked by subscribe on the value changed event, check my answer
            – MhmdRizk
            Nov 20 at 9:03




            thanks for the effort bro, but I posted the solution and it worked by subscribe on the value changed event, check my answer
            – MhmdRizk
            Nov 20 at 9:03


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381212%2fhow-to-subscribe-on-the-value-changed-control-event-of-a-uiswitch-using-rxswift%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin