views:

71

answers:

1

Hi, i used

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 180, 180)];
    myLabel.backgroundColor = [UIColor greenColor];
    [self.view addSubview:myLabel];

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self addTarget:self action:@selector(show)  forControlEvents:UIControlEventTouchDown];
    myButton.frame = CGRectMake(80.0, 120.0, 30, 30);
    myButton.showsTouchWhenHighlighted = YES;
    [self addSubview:myButton];

in this way the button works(call show:), but use [myLabel addSubview:myButton]; the button can't work. who can tell me why? I have tried to change the target to myLabel, can't work too.So who can tell me,thank you.

+1  A: 

UILabel by default doesn't handle any events. You need to set the userInteractionEnabled property of the label to YES.

A button shouldn't be a subview of a label anyway, it's illogical. Make both of them subviews of a UIView.

KennyTM
Thank you, just think they are all subclasses of UIView.
@william: UILabel *is* a subclass of UIView, only that it explicitly turns off `userInteractionEnabled`.
KennyTM