其他人看到这个问题了吗?我正在使用一个分段控件,并且我已经覆盖了它,这样当用户点击相同的段(索引)时,它就会被取消选择。
这在以前的版本中工作得很好,但现在在iOS5上进行测试。我发现,当您点击相同的网段时,UIControlEventValueChanged不会发送。因此,当你点击不同的区段时,代码可以正常工作,但对于同一区段则不是这样。
我的代码。
segmentCtrl = [[MySegmentedControl alloc] initWithItems: segmentCtrlLabels];
segmentCtrl.segmentedControlStyle = UISegmentedControlStyleBar;
// Register for touch events
[segmentCtrl addTarget:self action:@selector(segmentedCtrlTouched:) forControlEvents:UIControlEventValueChanged];我尝试注册UIControlEventTouchUpInside,并获得相同的行为。
对工作有什么建议吗?
你好,燕仪
发布于 2011-10-19 00:40:34
通过注册一个触摸事件修复了这个问题。如果接触到的片段相同,我将手动发送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];
}
}发布于 2013-01-30 13:44:20
是的,你需要自己实现控件事件。
- (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];
}https://stackoverflow.com/questions/7774683
复制相似问题