如何在UISegementedControl上注册UIControlEventTouchUpInside?

时间:2010-10-22 16:33:55

标签: iphone interface-builder ios uisegmentedcontrol

我想在事件UISegmentedControl - 而不是 UIControlEventTouchUpInside的{​​{1}}上注册一个功能。我特别想确保它仅在用户触摸项目时触发,而不是在以编程方式设置其值时触发。

有什么建议吗?

2 个答案:

答案 0 :(得分:5)

我发现,密钥是UISegmentedControl的子类。在正常情况下,UISegmentedControl根本不响应UIControlEventTouchUpInside。通过子类化UISegmentedControl,您可以重新定义函数touchesEnded(这实质上是用户触摸对象时触发的内容):

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

    MyViewController *vc; // the view controller which contains the UISegmentedControl
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[MyViewController class]]) {
            vc = (MyViewController*)nextResponder;
            break;
        }
    }

    if (vc) {
        [vc myFunction];
    }
} 

答案 1 :(得分:3)

我发现我能够在UISegmentedControl上使用UITapGestureRecognizer,我认为应该可以解决你的问题。