按两次相同的段后,UISegmentedController冻结

Sou*_*ock 5 objective-c uisegmentedcontrol ios swift

我结合Swift代码和第三方库(用Obj-C编写).我有一个带有UISegmentedController的UIViewController,我想在每次推送一个段或再次推送同一段时触发.

在我的Swift代码中,我有以下内容:

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")
        }
    }
Run Code Online (Sandbox Code Playgroud)

在库中我添加了以下代码:

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

所以基本上我想在每次用户按下一个段时触发一个ValueChanged动作(即使它是同一个段).目前,当我按下相同的段时,它会第二次触发,但之后UISegmentController变得无响应(无法再切换段).

Sou*_*ock 3

最终对我有用的是:

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

    [self sendActionsForControlEvents:UIControlEventTouchUpInside];
}
Run Code Online (Sandbox Code Playgroud)

carbonTabSwipeNavigation.carbonSegmentedControl!.addTarget(self, action: #selector(OverviewFolder.changesMade), forControlEvents: UIControlEvents.TouchUpInside)
Run Code Online (Sandbox Code Playgroud)