UISegmentedController freezes after clicking the same segment twice

I combine Swift code and a third-party library (written in Obj-C). I have a UIViewController with U UISegmentedController in which I want to run every time a segment has been clicked, or the same segment has been clicked again .

In my Swift code, I have the following:

override func viewDidLoad() { super.viewDidLoad() //setup items = ["newTab".localized,"topTab".localized,"categoryTab".localized] carbonTabSwipeNavigation = CarbonTabSwipeNavigation(items: items as [AnyObject], delegate: self) carbonTabSwipeNavigation.insertIntoRootViewController(self) self.style() self.view.userInteractionEnabled = true carbonTabSwipeNavigation.carbonSegmentedControl!.addTarget(self, action: #selector(OverviewFolder.changesMade), forControlEvents: UIControlEvents.ValueChanged) } func changesMade() { switch carbonTabSwipeNavigation.carbonSegmentedControl!.selectedSegmentIndex { case 0: print("tab 1") case 1: print("tab 2") case 2: print("tab 3") default: print("nope") } } 

In the library, I added the following code:

 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSInteger current = self.selectedSegmentIndex; [super touchesEnded:touches withEvent:event]; if (current == self.selectedSegmentIndex) [self sendActionsForControlEvents:UIControlEventValueChanged]; } 

So basically I want to trigger the ValueChanged action every time the user clicks on a segment (even if he is the same segment). It currently starts the second time I click on the same segment, but after that the UISegmentController becomes immune (can no longer switch segments).

+5
source share
2 answers

Which finally helped me with the following:

 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesEnded:touches withEvent:event]; [self sendActionsForControlEvents:UIControlEventTouchUpInside]; } 

and

 carbonTabSwipeNavigation.carbonSegmentedControl!.addTarget(self, action: #selector(OverviewFolder.changesMade), forControlEvents: UIControlEvents.TouchUpInside) 
+3
source

You should use the touchesEnded function, which is called when the user removes a finger from the screen and uses sendActions:

Objective-c

 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesEnded:touches withEvent:event]; [self sendActionsForControlEvents:UIControlEventTouchUpInside]; } 

Swift

 override func touchesEnded(_ touches: Set<AnyHashable>, withEvent event: UIEvent) { super.touchesEnded(touches, withEvent: event) self.sendActions(for: .touchUpInside) } 
+2
source

Source: https://habr.com/ru/post/1259541/


All Articles