UIButton with custom view - addTarget:action:forControlEvents: does not work
Solution 1
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];
Click Method
-(void)click
{
NSLog(@"%s",__FUNCTION__);
}
Solution 2
Swift 4.2 Solution
This is the solution of the problem (based on previous answers) with the last version of 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.
For seeing this more clearly, simply use the "Debug View Hierarchy" while debugging:
Solution 3
Rather than creating the customView (an instance of UIView) , you add customView as a instance of UIControl as also addTarget to the 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
-(void)customViewClicked:(id)sender
{
UIControl *senderControl = (UICotrol *)sender;
NSLog(@"sender control = %@",senderControl);
}
Hope it will help you.
Sagrian
Updated on October 17, 2020Comments
-
Sagrian over 3 years
My button is as follows
- Created label1
- Created label2
- Created customView (
UIView
) - Added label1 and label2 on custom view
- Created myCustomButton(
UIButton
) - Added customView on myCustomButton
I have already done userInteractionEnable for custom_View, label1 and label2.
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?
-
Hashem Aboonajmi over 9 yearsare you sure the View's
setUserInteractionEnabled
should be NO? I set it to yes, then it worked! -
Dipen Panchasara over 9 yearsyes, Because button is added in
self.view
and labels are inview
, thats why if you disable user interaction of it. it doesn't make any difference, as button & labels are in different container.