-->

UIButton's addtarget: not called after touch i

2020-07-27 04:42发布

问题:

I have the following code, and when i press the UIButton, nothing is called, and it doesn't crash.

    calloutButton = [[UIView alloc] initWithFrame:CGRectMake(left_width2*2-3, 5, 230, 230)];
    UIButton *buttongo= [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    buttongo.frame=CGRectMake(0, -1, 25, 25);
    [buttongo addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
    [calloutButton addSubview:buttongo];

    [label addSubview:calloutButton];




    -(IBAction)buttonEvent:(id)sender 
    {
          NSLog(@"Hello...");
    }

Someone Knows why?

Thanks!

回答1:

Check the size of the label, it can be CGSizeZero, but because the clipping of subviews is NO by default so the button is visible, but it isn't touchable.



回答2:

Make sure you not have

@property IBOutlet UIButton *yourButton;

with same name with

@implementation {
    UIButton *_yourButton;
}

I had such problem