xcode UIButton 的 addtarget: 触摸后不调用它!

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4908879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:10:55  来源:igfitidea点击:

UIButton's addtarget: not called after touch it!

iphoneobjective-cxcodeuibutton

提问by saimonx

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

我有以下代码,当我按下 UIButton 时,没有调用任何内容,也不会崩溃。

    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!

谢谢!

回答by Infinite Possibilities

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.

查看label的大小,可以是CGSizeZero,但是因为subviews的裁剪默认是NO所以按钮是可见的,但是是不可触摸的。

回答by Alexmelyon

Make sure you not have

确保你没有

@property IBOutlet UIButton *yourButton;

with same name with

同名

@implementation {
    UIButton *_yourButton;
}

I had such problem

我有这样的问题