UIButton's addtarget: not called after touch it!

别来无恙 提交于 2020-01-13 06:28:31

问题


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



来源:https://stackoverflow.com/questions/4908879/uibuttons-addtarget-not-called-after-touch-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!