简体   繁体   English

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

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

My button is as follows 我的按钮如下

  1. Created label1 创建的标签1
  2. Created label2 创建的标签2
  3. Created customView ( UIView ) 创建了customView( UIView
  4. Added label1 and label2 on custom view 在自定义视图上添加了label1和label2
  5. Created myCustomButton( UIButton ) 创建了myCustomButton( UIButton
  6. Added customView on myCustomButton 在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? 有什么办法吗?

Just a minor problem with your code my friend, you just need to add only one following line to your code, forget to setUserInteractionEnabled:NO to UIView it 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__);
}

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. 希望对您有帮助。

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 customView is definitely needed, since it is added on top and will eventually interfere with the tap gesture that we want to catch. 注意 :绝对需要禁用customView的用户交互,因为它是添加在顶部的,最终将干扰我们要捕获的轻customView手势。

For seeing this more clearly, simply use the "Debug View Hierarchy" while debugging: 为了更清楚地看到这一点,在调试时只需使用“ Debug View Hierarchy”:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UIButton addTarget:action:forControlEvents:[NSObject doesNotRecognizeSelector:]中的结果 - UIButton addTarget:action:forControlEvents: results in [NSObject doesNotRecognizeSelector:] UIButton 块相当于 addTarget:action:forControlEvents: 方法? - UIButton block equivalent to addTarget:action:forControlEvents: method? addTarget:action:forControlEvents:来自UITableViewCell - addTarget:action:forControlEvents: from a UITableViewCell 将参数传递给 addTarget:action:forControlEvents - Passing parameters to addTarget:action:forControlEvents 在.xib文件中创建的UIButton是IBAction还是[UIButton addTarget:action:forControlEvents:]? 哪种方法更好? - IBAction or [UIButton addTarget:action: forControlEvents:] for UIButton created in .xib file? Which is the better approach? UIControl(addTarget:action:forControlEvents :) @selector参数 - UIControl (addTarget:action:forControlEvents:) @selector arguments 使用addTarget获取运行时异常:Action:forControlEvents - Getting an runtime exception using addTarget:Action:forControlEvents addTarget:action:forControlEvents:您可以传递字典还是其他任何东西 - addTarget:action:forControlEvents: can you pass a dictionary or anything UIControl-addTarget:操作:forControlEvents不调用选择器内的方法 - UIControl - addTarget: action: forControlEvents doesn't call the method inside selector 在[UIButton addTarget]中传递自定义数据 - Passing custom data in [UIButton addTarget]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM