10

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?

Ankur
  • 5,086
  • 19
  • 37
  • 62
Sagrian
  • 1,048
  • 2
  • 11
  • 29

3 Answers3

27

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__);
}
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
  • are you sure the View's `setUserInteractionEnabled` should be NO? I set it to yes, then it worked! – Hashem Aboonajmi Oct 01 '14 at 18:59
  • yes, Because button is added in `self.view` and labels are in `view`, thats why if you disable user interaction of it. it doesn't make any difference, as button & labels are in different container. – Dipen Panchasara Oct 02 '14 at 05:53
4

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.

Gaurav Rastogi
  • 2,145
  • 1
  • 14
  • 19
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:

enter image description here

Alessandro Francucci
  • 1,528
  • 17
  • 25