extension of UISegmentedControl for touches events.

extension of UISegmentedControl for touches events in iOS 12 is working but in iOS 13 and in iOS 14 is not working, Why?

Can anyone tell me is there any change from iOS 12 version to iOS 13 and 14? or any one is facing same issue?

I use this code:

@interface UISegmentedControl (CustomSegmentControl)


@end

@implementation UISegmentedControl (CustomSegmentControl)


  • (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    NSInteger current = self.selectedSegmentIndex;

    [super touchesBegan:touches withEvent:event];

    if (current == self.selectedSegmentIndex)

        [self sendActionsForControlEvents:UIControlEventValueChanged];

    [self.nextResponder touchesBegan:touches withEvent:event];

}


  • (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self setSelectedSegmentIndex:self.selectedSegmentIndex];

    [super touchesEnded:touches withEvent:event];

    [self.nextResponder touchesEnded:touches withEvent:event];

}



@end
That seems to be a triplicate.

Please take care when you post on the forum.
It is my first post and when I was editing, updating the post it happened like this...@Claude31
It is my first post and when I was editing, updating the post it happened like this
So welcome to the forum.
For editing properly, you have to:
  • paste code with Paste and Match Style (avoid insertion of a lot of blank lines)

  • remove useless blank lines

  • Use the code formatter (<>)

Here is the result
Code Block
@interface UISegmentedControl (CustomSegmentControl)
@end
@implementation UISegmentedControl (CustomSegmentControl)
(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSInteger current = self.selectedSegmentIndex;
[super touchesBegan:touches withEvent:event];
if (current == self.selectedSegmentIndex)
[self sendActionsForControlEvents:UIControlEventValueChanged];
[self.nextResponder touchesBegan:touches withEvent:event];
}
(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self setSelectedSegmentIndex:self.selectedSegmentIndex];
[super touchesEnded:touches withEvent:event];
[self.nextResponder touchesEnded:touches withEvent:event];
}
@end


extension of UISegmentedControl for touches events in iOS 12 is working but in iOS 13 and in iOS 14 is not working, Why? 

  • What did you expect ?

  • What do you get ?

One remark:
  • in tutorials from Apple, super is called as the first statement of the override func

Code Block
// Implemented in your custom subclass
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event]; if ([touches count] != 1) {
self.state = UIGestureRecognizerStateFailed;
return;
}
}


Thanks @Claude31, for making me understand how to post code in forum. I tried already this " super is called as the first statement of the override func". In iOS 12 it is they are getting triggered whenever I touch on UISegmentedControl but in iOS 13.0 and 14.0 it is not even triggering the touches events methods in Custom Control. Does anyone have any idea on that?

whenever I touch on UISegmentedControl but in iOS 13.0 and 14.0 it is not even triggering the touches events methods in Custom Control

Do you mean touchesBegan is not called ?
Could you put a print("Began") ion line 9 ?
Yes, I debugged it and by putting NSLog(@"TouchesBegan Called"); but it is not getting called in iOS 13 and iOS 14 too.
Sorry for this probably too obvious point.

have you checked you set the class of the UISegmentedControl to CustomSegmentControl in IB ?
extension of UISegmentedControl for touches events.
 
 
Q