具有自定义视图的UIButton-addTarget:action:forControlEvents:不起作用 [英] UIButton with custom view - addTarget:action:forControlEvents: does not work

查看:113
本文介绍了具有自定义视图的UIButton-addTarget:action:forControlEvents:不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的按钮如下

  1. 创建的标签1
  2. 创建的标签2
  3. 创建的customView(UIView)
  4. 在自定义视图上添加了label1和label2
  5. 创建了myCustomButton(UIButton)
  6. 在myCustomButton上添加了customView
  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

我已经为custom_View,label1和label2完成了userInteractionEnable.

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

然后添加

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

还有

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

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

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

推荐答案

我的朋友,您的代码只是一个小问题,您只需要在代码中添加以下一行,就不用setUserInteractionEnabled:NOUIView了将允许您单击按钮

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

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__);
}

这篇关于具有自定义视图的UIButton-addTarget:action:forControlEvents:不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆