简体   繁体   中英

How do I observe changes in a UIControl subclass in ReactiveSwift?

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM