UISegmentedControl register taps on selected segment

后端 未结 20 3073
渐次进展
渐次进展 2020-12-04 15:28

I have a segmented control where the user can select how to order a list. Works fine.

However, I would like that when an already selected segment is tapped, the orde

20条回答
  •  醉酒成梦
    2020-12-04 16:04

    Wanted this myself. Took Julian's solution (thanks!) and modified slightly.

    The following UISegmentedControl subclass simply triggers the UIControlEventValueChanged event even when the value didn't change, which obviously reads a bit weird, but works fine in my case and keeps things simple.

    AKSegmentedControl.h

    #import 
    
    @interface AKSegmentedControl : UISegmentedControl {
    }
    
    @end
    

    AKSegmentedControl.m

    #import "AKSegmentedControl.h"
    
    @implementation AKSegmentedControl
    
    - (void)setSelectedSegmentIndex:(NSInteger)toValue {
      // Trigger UIControlEventValueChanged even when re-tapping the selected segment.
      if (toValue==self.selectedSegmentIndex) {
        [self sendActionsForControlEvents:UIControlEventValueChanged];
      }
      [super setSelectedSegmentIndex:toValue];        
    }
    
    @end
    

提交回复
热议问题