10

我的按钮如下

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

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

然后添加

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

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

但即使我触摸按钮,也不会调用上述函数。有什么解决办法吗?

4

3 回答 3

27

我的朋友,你的代码只是一个小问题,你只需要在你的代码中添加以下一行,忘记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];

点击方法

-(void)click
{
    NSLog(@"%s",__FUNCTION__);
}
于 2013-05-08T14:22:18.713 回答
3

与其创建 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];

现在在 CustomViewClicked 方法中

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

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

希望它会帮助你。

于 2013-05-08T14:04:47.273 回答
3

斯威夫特 4.2 解决方案

这是 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")
}

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

为了更清楚地看到这一点,只需在调试时使用“Debug View Hierarchy”:

在此处输入图像描述

于 2018-10-16T12:09:24.260 回答