【问题标题】:UISegmentedControl not firing with iOS 5UISegmentedControl 未在 iOS 5 中触发
【发布时间】:2011-12-08 03:34:51
【问题描述】:

还有其他人看到这个问题吗?我正在使用分段控件,并且已将其覆盖,因此当用户点击相同的段(索引)时,它会被取消选择。

这在以前的版本中运行良好,但现在在 iOS5 上进行测试。而且我发现当您点击同一段时未发送 UIControlEventValueChanged 。因此,当您点击不同的段时,代码可以正常工作,但不能用于同一段。

我的代码。

segmentCtrl = [[MySegmentedControl alloc] initWithItems: segmentCtrlLabels];
segmentCtrl.segmentedControlStyle = UISegmentedControlStyleBar;
// Register for touch events
[segmentCtrl addTarget:self action:@selector(segmentedCtrlTouched:) forControlEvents:UIControlEventValueChanged];

我尝试注册 UIControlEventTouchUpInside,并获得相同的行为。

有什么变通的建议吗?

问候, 夜翼

【问题讨论】:

  • 是的。这里有类似的问题。 iOS API diffs 列出了对 UISegmentedControl 的一些更改,但没有详细说明。我也想知道这笔交易到底是什么。

标签: ios5 uisegmentedcontrol


【解决方案1】:

是的,您需要自己实现控制事件。

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

    [self sendActionsForControlEvents: UIControlEventTouchDown];
}

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

    if (CGRectContainsPoint(self.bounds, [touches.anyObject locationInView: self]))
    {
        [self sendActionsForControlEvents: UIControlEventTouchUpInside];
    }
    else
    {
        [self sendActionsForControlEvents: UIControlEventTouchUpOutside];
    }
}

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

    [self sendActionsForControlEvents: UIControlEventTouchCancel];
}

【讨论】:

    【解决方案2】:

    通过注册触摸事件来修复它。如果触摸的段相同,我手动发送 EventChanged 事件。

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

    【讨论】:

    • +1 我在代码中稍作修改,直接使用 [self setSelectedSegmentIndex:UISegmentedControlNoSegment]; 取消选择段
    猜你喜欢
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    相关资源
    最近更新 更多