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?
ios swift rx-swift
add a comment |
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?
ios swift rx-swift
DoesyourSwitch.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
add a comment |
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?
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 at 20:51
Damon
500317
500317
asked Nov 19 at 19:16
MhmdRizk
426310
426310
DoesyourSwitch.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
add a comment |
DoesyourSwitch.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
add a comment |
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)
add a comment |
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)
Looks good. Congrats. :)
– Glenn
Nov 20 at 9:11
add a comment |
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
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
add a comment |
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)
add a comment |
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)
add a comment |
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)
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 at 0:20
answered Nov 20 at 0:13
prekshya basnet
1368
1368
add a comment |
add a comment |
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)
Looks good. Congrats. :)
– Glenn
Nov 20 at 9:11
add a comment |
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)
Looks good. Congrats. :)
– Glenn
Nov 20 at 9:11
add a comment |
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)
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 at 7:18
MhmdRizk
426310
426310
Looks good. Congrats. :)
– Glenn
Nov 20 at 9:11
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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 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
add a comment |
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
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 at 19:20
I tried it now, and it didnt
– MhmdRizk
Nov 19 at 19:33