[英]UIButton with custom view - addTarget:action:forControlEvents: does not work
My button is as follows 我的按钮如下
UIView
) 创建了customView( UIView
) UIButton
) 创建了myCustomButton( UIButton
) 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:NO
为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];
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.