In the following code, I created a MTLabel
object and a UIButton
object, changed their frame, then add UIButton
as subView of 'MTLabel'
MTLabel* label = [MTLabel labelWithTitle:title];
label.frame = ...
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
action:@selector(didSelectStore:)
forControlEvents:UIControlEventAllEvents];
[label addSubview:rightButton];
rightButton.frame = ...;
[an addLabel:label];
After that both UIView
s appears on screen, howeverdidSelectStore:
is never called when touching that rightButton
.
As MTLabel
is defined in a third-party lib, I don't have it's source code. So I write a category like this:
@implementation MTLabel(button)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
}
......
@end
After doing that, [rightButton touchesBegan:event:]
is still not called, not mention that action.
So the touch events are not working as expected in that subView.
Any idea on how then happen and what s开发者_JAVA技巧hould I do?
Thank you.
I don't think you need to create a category and add touchesBegan
: to make this work. Have you ensured that label.userInteractionEnabled = YES;
?
精彩评论