I’ve subclassed a UILabel and added 2 properties that look like this:
@property (nonatomic, assign) SEL action;
@property (nonatomic, assign) id target;
I then implemented UIView’s touches began method like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([target respondsToSelector:@selector(action)]) {
[target performSelector:@selector(action) onThread:[NSThread mainThread] withObject:nil waitUntilDone:NO];
}
}
within the class containing the subclassed UILabel, i set the target and action like this:
label.target = self;
labek.action = @selector(myMethod);
label.userInteractionEnabled = YES;
The class including the label does indeed have the method myMethod, and so it should respond to it. Any ideas why it might not?
thanks!
You are setting your action like this
label.action = @selector(myMethod);and then using action and passing it into a second selector[target respondsToSelector:@selector(action)]. That will not work.You want to do this:
Basically, the if statement fails because the target does not respond to that selector. Therefore, it is never being called.