swiftreactive-cocoareactive-cocoa-5

Observe UISwich control state


Observing the switch and trigger action seems like below in reactive cocoa 4.But can't figure out in reactive cocoa 5.

            mediaTypeSwich.rac_signalForControlEvents(.ValueChanged)
                .flattenMap { (sender:AnyObject!) -> RACStream! in
                    let segment = sender as UISegmentedControl
                    switch segment.selectedSegmentIndex {
                    case 0:
                        return self.photoSignal()

                    default:
                        return self.videoSignal()
                    }
                }
                .deliverOnMainThread()
                .subscribeNextAs{(photosFetchResult: PHFetchResult) in
                    self.fetchResult = photosFetchResult
                    self.mediaCollectionView.reloadData()
            }

how can i do this for reactive cocoa 5

What i am trying to do is

testSwitch.reactive.trigger(for: .valueChanged)

And then

get the control enabled state and trigger action depending on the state.


Solution

  • I'm not sure what ur means, You want to listen the index value changed of the segmentedControl, don't u?

    You could try these:

    var selectedIndex = MutableProperty<NSInteger>(0)
    
    segControl.reactive
            .selectedSegmentIndexes
            .map({ $0 }).observeValues({ log.debug($0) })
    

    To monitor the UISwitch value changed, you could try this:

           let `switch` = UISwitch.init(frame: CGRect(x: 100, y: 100, width: 0, height: 0))
           self.view.addSubview(`switch`)
           `switch`.reactive.isOnValues.observeValues({ print($0) })