【问题标题】:Problems using UIPickerView delegate methods使用 UIPickerView 委托方法的问题
【发布时间】:2011-10-25 07:39:50
【问题描述】:
我有一个 UIPicker 并设置了我的委托方法。
我希望当用户点击选定的选择器项目时发生一些事情。处理它:
- (void)pickerView:(UIPickerView *)aPickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
触发选择选择器项未删除时发生的事件。
点击选定的选择器项目时如何触发事件?
【问题讨论】:
标签:
iphone
objective-c
cocoa-touch
uipickerview
【解决方案1】:
您必须继承 UIPickerView 并手动检测那里的触摸。是的,它看起来很乱。但这似乎是唯一的方法。下面的代码可能会有所帮助,
@interface TouchDetectionView : UIPickerView {
}
- (UIView *)getNextResponderView:(NSSet *)touches withEvent:(UIEvent *)event;
@end
@implementation TouchDetectionView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UIView * hitTestView = [self getNextResponderView:touches withEvent:event];
[hitTestView touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UIView * hitTestView = [self getNextResponderView:touches withEvent:event];
[hitTestView touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UIView * hitTestView = [self getNextResponderView:touches withEvent:event];
[hitTestView touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
UIView * hitTestView = [self getNextResponderView:touches withEvent:event];
[hitTestView touchesCancelled:touches withEvent:event];
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return self;
}
- (UIView *)getNextResponderView:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
UIView * hitTestView = [super hitTest:point withEvent:event];
return ( hitTestView == self ) ? nil : hitTestView;
}
参考:Responding to touchesBegan in UIPickerView instead of UIView