Frage

I have a subclass UIButton that loads a 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 a view, like this:

// 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 is tap: method won't be called. Also tried with a normal UIButton, and it did work, so it must be a problem with the custom xib file, I don't really know which view responding on the tap action first? How can I set the button to be the one to respond on tap action?

What did I missed?

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

Thank you!

War es hilfreich?

Lösung

Disable the subviews of your view because they catch the touches.
Either in the xib (userinteraction enabled) or by code:

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top