How to subscribe on the value changed control event of a UISwitch Using Rxswift
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
add a comment |
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
DoesyourSwitch.rx.value.changed.subscribe(onNext: ...)
work?
– Sweeper
Nov 19 '18 at 19:20
I tried it now, and it didnt
– MhmdRizk
Nov 19 '18 at 19:33
add a comment |
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
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
ios swift rx-swift
edited Nov 19 '18 at 20:51
Damon
517317
517317
asked Nov 19 '18 at 19:16
MhmdRizkMhmdRizk
436310
436310
DoesyourSwitch.rx.value.changed.subscribe(onNext: ...)
work?
– Sweeper
Nov 19 '18 at 19:20
I tried it now, and it didnt
– MhmdRizk
Nov 19 '18 at 19:33
add a comment |
DoesyourSwitch.rx.value.changed.subscribe(onNext: ...)
work?
– Sweeper
Nov 19 '18 at 19:20
I tried it now, and it didnt
– MhmdRizk
Nov 19 '18 at 19:33
Does
yourSwitch.rx.value.changed.subscribe(onNext: ...)
work?– Sweeper
Nov 19 '18 at 19:20
Does
yourSwitch.rx.value.changed.subscribe(onNext: ...)
work?– Sweeper
Nov 19 '18 at 19:20
I tried it now, and it didnt
– MhmdRizk
Nov 19 '18 at 19:33
I tried it now, and it didnt
– MhmdRizk
Nov 19 '18 at 19:33
add a comment |
3 Answers
3
active
oldest
votes
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)
add a comment |
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)
Looks good. Congrats. :)
– Glenn
Nov 20 '18 at 9:11
add a comment |
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
this worked for me thanks , but I preferred subscribing on the control event it self, not the value
– MhmdRizk
Nov 20 '18 at 7:19
See my updated answer. :)
– Glenn
Nov 20 '18 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 '18 at 9:03
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
add a comment |
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)
add a comment |
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)
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)
edited Nov 20 '18 at 0:20
answered Nov 20 '18 at 0:13


prekshya basnetprekshya basnet
466110
466110
add a comment |
add a comment |
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)
Looks good. Congrats. :)
– Glenn
Nov 20 '18 at 9:11
add a comment |
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)
Looks good. Congrats. :)
– Glenn
Nov 20 '18 at 9:11
add a comment |
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)
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)
answered Nov 20 '18 at 7:18
MhmdRizkMhmdRizk
436310
436310
Looks good. Congrats. :)
– Glenn
Nov 20 '18 at 9:11
add a comment |
Looks good. Congrats. :)
– Glenn
Nov 20 '18 at 9:11
Looks good. Congrats. :)
– Glenn
Nov 20 '18 at 9:11
Looks good. Congrats. :)
– Glenn
Nov 20 '18 at 9:11
add a comment |
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
this worked for me thanks , but I preferred subscribing on the control event it self, not the value
– MhmdRizk
Nov 20 '18 at 7:19
See my updated answer. :)
– Glenn
Nov 20 '18 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 '18 at 9:03
add a comment |
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
this worked for me thanks , but I preferred subscribing on the control event it self, not the value
– MhmdRizk
Nov 20 '18 at 7:19
See my updated answer. :)
– Glenn
Nov 20 '18 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 '18 at 9:03
add a comment |
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
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
edited Nov 20 '18 at 8:55
answered Nov 19 '18 at 19:42


GlennGlenn
4,86421740
4,86421740
this worked for me thanks , but I preferred subscribing on the control event it self, not the value
– MhmdRizk
Nov 20 '18 at 7:19
See my updated answer. :)
– Glenn
Nov 20 '18 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 '18 at 9:03
add a comment |
this worked for me thanks , but I preferred subscribing on the control event it self, not the value
– MhmdRizk
Nov 20 '18 at 7:19
See my updated answer. :)
– Glenn
Nov 20 '18 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 '18 at 9:03
this worked for me thanks , but I preferred subscribing on the control event it self, not the value
– MhmdRizk
Nov 20 '18 at 7:19
this worked for me thanks , but I preferred subscribing on the control event it self, not the value
– MhmdRizk
Nov 20 '18 at 7:19
See my updated answer. :)
– Glenn
Nov 20 '18 at 8:55
See my updated answer. :)
– Glenn
Nov 20 '18 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 '18 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 '18 at 9:03
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Does
yourSwitch.rx.value.changed.subscribe(onNext: ...)
work?– Sweeper
Nov 19 '18 at 19:20
I tried it now, and it didnt
– MhmdRizk
Nov 19 '18 at 19:33