问题
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?
回答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__);
}
回答2:
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.
回答3:
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:
来源:https://stackoverflow.com/questions/16442559/uibutton-with-custom-view-addtargetactionforcontrolevents-does-not-work