6

While detecting changes works with UIControlEventValueChanged - I need to detect touches even on selected segments.

I tried

   [onOffSC addTarget:self
            action:@selector(segmentedControlPushed)
  forControlEvents:UIControlEventAllTouchEvents];

But this fires nothing.

Is there a way to detect touches on a selected segment?

EDIT - without having to create a new subclass. ps also the gesture recognizer does not accept the segmentcontrol when trying to drag it there

Many thanks

user387184
  • 10,953
  • 12
  • 77
  • 147

1 Answers1

2

I think this will work

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSInteger oldValue = self.selectedSegmentIndex;
    [super touchesBegan:touches withEvent:event];
    if ( oldValue == self.selectedSegmentIndex )
        [self sendActionsForControlEvents:UIControlEventValueChanged];
}
Brian Sachetta
  • 3,319
  • 2
  • 34
  • 45
Sebastian Łuczak
  • 1,116
  • 8
  • 19
  • for this I have to create a new subclass... - I forgot to mention it initially – user387184 Jul 20 '12 at 07:38
  • You should be able to connect Tap Gesture Recognizer to SegmentedControl in IB, then just connect your own method - (IBAction)segmentedControlTouched:(id)sender. I'm pretty sure this method will fire everytime you touch SegmentedControl. – Sebastian Łuczak Jul 20 '12 at 07:51
  • ..as I mentioned above, for some reason this does not work - possibly bec I am using Xcode 4.3???? – user387184 Jul 20 '12 at 08:06
  • 1
    See this answer for an iOS 7 updated version: http://stackoverflow.com/a/17652998/292145 – Klaas Sep 24 '13 at 15:22