ios 带有自定义视图的 UIButton - addTarget:action:forControlEvents: 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16442559/
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-08-30 23:30:36  来源:igfitidea点击:

UIButton with custom view - addTarget:action:forControlEvents: does not work

iphoneiosuibuttonuitouch

提问by Sagrian

My button is as follows

我的按钮如下

  1. Created label1
  2. Created label2
  3. Created customView (UIView)
  4. Added label1 and label2 on custom view
  5. Created myCustomButton(UIButton)
  6. Added customView on myCustomButton
  1. 创建标签1
  2. 创建标签2
  3. 创建自定义视图 ( UIView)
  4. 在自定义视图上添加了 label1 和 label2
  5. 创建了 myCustomButton( UIButton)
  6. 在 myCustomButton 上添加了 customView

I have already done userInteractionEnable for custom_View, label1 and label2.

我已经为 custom_View、label1 和 label2 完成了 userInteractionEnable。

Then added

然后添加

[myCustomButton addTarget:self action:@selector(OnButtonClick:) forControlEvents:UIControlEventTouchUpInside];

And

-(void)OnButtonClick:(UIButton *)sender
{
}

But above function is never called even when I touch the button. Any solution?

但是即使我触摸按钮,也永远不会调用上述功能。有什么解决办法吗?

回答by Dipen Panchasara

Just a minor problem with your code my friend, you just need to add only one following line to your code, forget to setUserInteractionEnabled:NOto UIViewit will allow you to click the button

只需用代码一个小问题我的朋友,你只需要添加只有一个下面一行到你的代码,忘了setUserInteractionEnabled:NOUIView它可以让你按一下按钮

UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
[lbl1 setText:@"ONe"];
UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 100, 30)];
[lbl2 setText:@"Two"];

UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 130)];
[view setUserInteractionEnabled:NO];

[view addSubview:lbl1];
[view addSubview:lbl2];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addSubview:view];
[btn setFrame:CGRectMake(0, 0, 200, 130)];
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

Click Method

点击方式

-(void)click
{
    NSLog(@"%s",__FUNCTION__);
}

回答by Gaurav Rastogi

Rather than creating the customView (an instance of UIView) , you add customView as a instance of UIControl as also addTarget to the customView ,

而不是创建 customView (UIView 的一个实例),你添加 customView 作为 UIControl 的实例,也 addTarget 到 customView ,

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(10,10,300,300)];

UIControl *customView = [[UIControl alloc] initWithFrame:CGRectMake(0,0,300,300)];
[customView addTarget:self action:@selector(customViewClicked:) forControlEvents:UIControlEventTouchUpInside]; 

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10,10,100,100)];
[label1 setText:@"Hello How are you ?"];

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10,150,100,100)];
[label1 setText:@"I am fine Thnank You!"]

[customView addSubView:lebel1];
[customView addSubView:lebel2];

Now in the CustomViewClicked Method

现在在 CustomViewClicked 方法中

-(void)customViewClicked:(id)sender
{
     UIControl *senderControl = (UICotrol *)sender;

     NSLog(@"sender control = %@",senderControl);
}

Hope it will help you.

希望它会帮助你。

回答by Alessandro Francucci

Swift 4.2 Solution

Swift 4.2 解决方案

This is the solution of the problem (based on previous answers) with the last version of Swift:

这是最新版本的 Swift 问题的解决方案(基于之前的答案):

func customButton() {

    // Label Creation
    let firstLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
    firstLabel.text = "first"

    let secondLabel = UILabel(frame: CGRect(x: 0, y: 30, width: 100, height: 30))
    secondLabel.text = "second"

    // Custom View creation
    let viewFrame = CGRect(x: 0, y: 0, width: 100, height: 60)
    let customView = UIView(frame: viewFrame)
    customView.addSubview(firstLabel)
    customView.addSubview(secondLabel)
    customView.isUserInteractionEnabled = false

    // Custom button
    let button = UIButton(type: .custom)
    button.frame = viewFrame
    button.addSubview(customView)

    button.addTarget(self, action: #selector(click), for: .touchUpInside)
    addSubview(button)
}

@objc
func click() {
    print("Click")
}

Notes: Disabling the user interaction of the customViewis definitely needed, since it is added on top and will eventually interfere with the tap gesture that we want to catch.

注意:禁用 的用户交互customView是绝对需要的,因为它被添加到顶部并且最终会干扰我们想要捕捉的点击手势。

For seeing this more clearly, simply use the "Debug View Hierarchy" while debugging:

为了更清楚地看到这一点,只需在调试时使用“调试视图层次结构”:

enter image description here

在此处输入图片说明