我对 UILabel 进行了子类化,并添加了 2 个如下所示的属性:
@property (nonatomic, assign) SEL action;
@property (nonatomic, assign) id target;
然后我实现了 UIView 的触摸开始方法,如下所示:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([target respondsToSelector:@selector(action)]) {
[target performSelector:@selector(action) onThread:[NSThread mainThread] withObject:nil waitUntilDone:NO];
}
}
在包含子类 UILabel 的类中,我将目标和操作设置如下:
label.target = self;
labek.action = @selector(myMethod);
label.userInteractionEnabled = YES;
包含标签的类确实有方法 myMethod,所以它应该响应它。任何想法为什么它可能不会?
谢谢!