El evento UIButton touchUpInside no se dispara correctamente

I have a tableview that contains a row with a custom cell that contains a UIButton. However, the button doesn't always fire the action. Here's my code:

   submitButton = [[UIButton alloc] init];

    [[submitButton layer] setBorderColor:[[UIColor whiteColor] CGColor]];
    [submitButton setClipsToBounds: YES];
    submitButton.backgroundColor = [UIColor grayColor];
    [submitButton setTitle:@"Send" forState:UIControlStateNormal];
    [self.contentView addSubview:submitButton];
    [submitButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [submitButton release];

This is called in the custom cell's -(id)initWithStyle:

La buttonAction el método se ve así:

 -(void)buttonAction
 {
     NSLog(@"Button Clicked!");
 }

It seems that the only way I can get the buttonAction to fire is if I press down on the button and release somewhere inside the cell's frame, but not inside the button itself. Why would that be?

*ACTUALIZACIÓN* Problem still exists, but I found that the more consistent way to get the button to fire is to click and drag to the left or right and then let go, as long as I let go within the bounds of the cell/row.

ACTUALIZACIÓN #2 It looks like if I use iOS 6.0, it works as intended. But on 5.0 or 5.1 it does not.

preguntado el 09 de enero de 13 a las 19:01

alloc-init on UIButton creates a button of type UIButtonTypeCustom, of which the default properties and settings make absolutely no damn sense. At least try setting an appropriate frame to see if it works. -

I set a frame in a later method using [submitButton setFrame:CGRectMake(centerCalc, 0, 200, 65)]; The button shows up fine, that is not the problem. Also, if I give the button images using setImage:forState: it properly displays the images depending on whether the I've pressed on the button or now. -

What else is in this cell? Any gesture recognizers? That could screw up a button... -

No other gesture recognizers in this cell. -

figured it out - I have a gesture recognizer on the main tableview for clicks on the background that calls a method to hide the keyboard. -

2 Respuestas

Try assigning the same method call to TouchUpOutside as well - you should then see it work every time. TouchUpInside is only fired if you lift your finger while still within the bounds of the button.

Respondido el 09 de enero de 13 a las 21:01

Yes, TouchUpOutside works as expected. But it's not what I'm looking for. I want users to be able to simply click on the button and have it work, not have to drag their finger off the button, or across the button. - Chris

Trata [submitButton sizeToFit]. I'm wondering whether your button has any size (since I don't see you giving it any).

Also: Create your button with [UIButton buttonWithType: UIButtonTypeCustom] instead of alloc-init.

Respondido el 09 de enero de 13 a las 22:01

I set the frame in a later method, using: [submitButton setFrame:CGRectMake(centerCalc, 0, 200, 65)]; - Chris

OK but hiding relevant code from people who you are asking to help you is not very nice. - mate

sorry - wasn't trying to be mean - it just didn't seem like that other method would be relevant here. - Chris

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.