I am trying to have notifications when a button changes his 'isSelected' state.
I have an UIButton setup with a target to change his isSelected:
let button = UIButton()
button.addTarget(button, action: #selector(toggleSelected), for: .touchUpInside)
....
@objc func toggleSelected() {
self.isSelected = !self.isSelected
}
and I want to have something like button.rx.isSelectedChanged to be able to subscribe events.
I tried to extend UIButton.rx with a ControlProperty:
extension Reactive where Base: UIButton {
public var isSelectedChanged: ControlProperty<Bool> {
return base.rx.controlProperty(
editingEvents: [.allEditingEvents, .valueChanged],
getter: { $0.isSelected },
setter: { $0.isSelected = $1 })
}
}
and something like:
button.rx.observe(Bool.self, "isSelected")
.takeUntil(button.rx.deallocated)
.subscribe(onNext: { print("selcted \($0)") })
but in neither way I have button.isSelected updates.
What is a good way of doing this?
EDIT: Following @daniel-t reply I implemented it here.