UISegmentedControl register taps on selected segment

后端 未结 20 3075
渐次进展
渐次进展 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 15:53

    Big help! What I want to do is have the option of one or no buttons set - and when a button is set, a second tap unsets it. This is my modification:

    - (void)setSelectedSegmentIndex:(NSInteger)toValue
    {
      // Trigger UIControlEventValueChanged even when re-tapping the selected segment.
      if (toValue==self.selectedSegmentIndex) {
        [super setSelectedSegmentIndex:UISegmentedControlNoSegment]; // notify first
        [self sendActionsForControlEvents:UIControlEventValueChanged]; // then unset
      } else {
        [super setSelectedSegmentIndex:toValue]; 
      }
    }
    

    Notify first lets you read the control to find the current setting before it's reset.

提交回复
热议问题