有没有人看到这个问题?我正在使用分段控件,并且我已经覆盖了它,以便当用户点击相同的段(索引)时,它将被取消选择。
这在之前的版本中运行良好,但现在在iOS5上进行测试。并且我发现当您点击同一段时不会发送UIControlEventValueChanged。因此,当您点击不同的段时,代码可以正常工作,但不适用于同一段。
我的代码。
segmentCtrl = [[MySegmentedControl alloc] initWithItems: segmentCtrlLabels];
segmentCtrl.segmentedControlStyle = UISegmentedControlStyleBar;
// Register for touch events
[segmentCtrl addTarget:self action:@selector(segmentedCtrlTouched:) forControlEvents:UIControlEventValueChanged];
我尝试注册UIControlEventTouchUpInside,并获得相同的行为。
有关解决方法的任何建议吗?
此致 延伊
答案 0 :(得分:1)
通过注册触摸事件来修复它。如果触摸的段相同,我手动发送事件更改事件。
- (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 :(得分:0)
是的,你需要自己实现控制事件。
- (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];
}