我的按钮如下
- 已创建标签1
- 创建标签2
- 创建了自定义视图(
UIView
)
- 在自定义视图上添加了 label1 和 label2
- 创建了 myCustomButton(
UIButton
)
- 在我的自定义按钮上添加了自定义视图
我已经为custom_View、label1和label2完成了userInteractionEnable。
然后添加
[myCustomButton addTarget:self action:@selector(OnButtonClick:) forControlEvents:UIControlEventTouchUpInside];
And
-(void)OnButtonClick:(UIButton *)sender
{
}
但即使当我触摸按钮时,上述函数也不会被调用。有什么解决办法吗?
我的朋友,您的代码只是一个小问题,您只需在代码中添加以下一行即可,忘记setUserInteractionEnabled:NO
to UIView
它将允许您单击按钮
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__);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)