How to add a tap action to a custom UIButton?

I have a subclass of UIButton that loads Nib:

@implementation BlaButtonVC - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { NSArray *subViews = [[NSBundle mainBundle] loadNibNamed:@"BlaButton" owner:self options:nil]; [self addSubview:subViews[0]]; } return self; } @end 

And I can add this button to the view, for example:

 // ViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. BlaButtonVC *button = [[BlaButtonVC alloc] initWithFrame:CGRectMake(10, 10, 280, 44)]; button.titleXLabel.text = @"Bls"; button.descLabel.text = @"Kues"; [button addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside]; [button becomeFirstResponder]; [self.view addSubview:button]; } -(IBAction)tap:(id)sender { NSLog(@"sssss"); } 

My problem: tap: the method will not be called. They also tried with a regular UIButton, and it really worked, so this should be a problem with a custom xib file, I really donโ€™t know which view responds to the touch action first? How can I configure a button to respond to a click action?

What did I miss?

Here you can find the full xcode project:
https://www.dropbox.com/s/zjhwy7jm8jcywz4/blabla1_111.zip

Thanks!

+4
source share
1 answer

Turn off the subzones of your view because they will catch the strokes.
Either in xib (userinteraction userinteraction enabled ), or by code:

((UIView *)subViews[0]).userInteractionEnabled = NO;

+2
source

All Articles