Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton with custom view - addTarget:action:forControlEvents: does not work

My button is as follows

  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

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?

like image 571
Sagrian Avatar asked May 08 '13 13:05

Sagrian


3 Answers

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:

enter image description here

like image 70
Alessandro Francucci Avatar answered Nov 18 '22 16:11

Alessandro Francucci


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__);
}
like image 22
Dipen Panchasara Avatar answered Nov 18 '22 15:11

Dipen Panchasara


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.

like image 3
Gaurav Rastogi Avatar answered Nov 18 '22 16:11

Gaurav Rastogi