【问题标题】:UISegmentedControl: how to detect click on current segment?UISegmentedControl:如何检测当前段的点击?
【发布时间】:2013-11-24 00:12:00
【问题描述】:

有没有办法检测对 UISegmentedControl 中的段的第二次点击?我发现:

Detect second click on a segment

但是,声明是:

如果您将分段控件设置为具有瞬时样式,则当用户触摸该分段时,该分段不会显示为选中状态(蓝色背景)。披露按钮始终是瞬时的,不会影响实际选择。

有没有办法检测第二次点击以及触发选择操作并将片段显示为选中?

如果没有直接的方法,我在想,我首先将momentary 标志设置为YES,然后在每次点击时手动更新选择状态,但我也需要更新/取消选择其他段。

谢谢

【问题讨论】:

    标签: ios objective-c uisegmentedcontrol


    【解决方案1】:

    解决方案是有一个 UISegmentedControl 的自定义子类并像这样自己检查它。

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

    我在 touchesBegan 中有一个其他解决方案,但它在 iOS 7 中不再有效。Stack Overflow 上还有其他解决方案在 iOS 6 及更高版本中无效。

    【讨论】:

    • 到目前为止我尝试过的大多数解决方案都会触发两次选择事件,这在 iOS 7 上完美运行。你是最棒的,谢谢!
    【解决方案2】:

    无法再次使特定段可点击,但您可以使用 UISegmentedControlNoSegment 重置整个段控制。

    [self.segmentCtrlOutlet setSelectedSegmentIndex:UISegmentedControlNoSegment];
    

    你要做的就是把上面的代码放在当你点击 UISegmentedControl 的特定段时代码执行的地方。

    例如。在我的项目中,当我点击一个片段时,UIPopoverController 打开,里面有 UIPicker,所以我在 UIPicker 委托方法“didSelectRow”中使用上面的代码

    【讨论】:

    猜你喜欢
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多