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

时间:2013-05-08 13:53:58

标签: iphone ios uibutton uitouch

我的按钮如下

  1. 创建label1
  2. 创建label2
  3. 创建了customView(UIView
  4. 在自定义视图
  5. 上添加了label1和label2
  6. 创建了myCustomButton(UIButton
  7. 在myCustomButton上添加了customView
  8. 我已经为custom_View,label1和label2完成了userInteractionEnable。

    然后添加

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

    并且

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

    但即使我按下按钮,上面的功能也从未被调用过。任何解决方案?

3 个答案:

答案 0 :(得分:22)

我的朋友对您的代码只是一个小问题,您只需要在代码中添加一行,忘记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__);
}

答案 1 :(得分: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);
}

希望它会对你有所帮助。

答案 2 :(得分:1)

Swift 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”:

enter image description here