I've built a subclass of UIControl and I want to observe for toggling events. There are a few older answers out there from RAC3 days, we're up to RAC5 now so I figured I'd ask the question to get something more current.
答案 0 :(得分:4)
I think the most common way is by using reactive.mapControlEvents
. Basically, it creates a signal that fires everytime the UIControl
sends a control event, and map the UIControl
to focus on the property you are interested in.
Using this, you can then create your own signals based on your needs. For instance, here is what you would do if you want to create a signal that triggers when a UISegmentedControl
updates its index:
extension Reactive where Base: UISegmentedControl {
/// A signal of indexes of selections emitted by the segmented control.
public var selectedSegmentIndexes: Signal<Int, NoError> {
return mapControlEvents(.valueChanged) { $0.selectedSegmentIndex }
}
}
(this code is actually taken from ReactiveCocoa
directly).