Will Henderson
Will Henderson

Reputation: 502

UIButton touchUpInside action firing multiple times per click

I have three buttons set up in Interface Builder, each tied via touchUpInside to btnSelection:

- (IBAction)btnSelection:(id)sender {
    NSLog(@"tag: %i", [sender tag]);
}

In my console, the first click registers correctly (after a second or so delay, which seems a bit weird) but any subsequent touch of any of the 3 buttons results in first logging the previous tag value, then logging the new tag.

Touch #1 (btn w/ tag=0):

tag:0

Touch #2 (btn w/ tag=1):

tag:0 tag:1

Touch #3 (btn w/ tag=2):

tag:1 tag:2

and so on.

I can't figure out why two events are being logged each time (with the first being the previously touched button.

Upvotes: 3

Views: 4187

Answers (4)

Cameron Lowell Palmer
Cameron Lowell Palmer

Reputation: 22245

This is quite likely related to a mistake you have made in Interface Builder. I have seen this happen when for example a button in a view is clicked but the next view has some problem like a broken IBOutlet connect from when you copied and pasted some IB objects. Breakpointing the item would result in 3 calls to IBAction and then death.

Upvotes: 0

Chris Klingler
Chris Klingler

Reputation: 5296

I know this was not included in the question but in the case that you might have a uibutton in a custom uitableviewcell, make sure not to mess with selection settings when setting up your cells for reuse. (Messing with cells being set up for reuse can make all subviews in the cell go weird and make things pile up or make things like buttons appear to toggle when they are really reloading cells) Mess with those kinds of things when first customizing your cell format or after your cells are made

Upvotes: 1

Olie
Olie

Reputation: 24675

Put a breakpoint in your callback and see where the call is coming from. What you describe works great in 100s of 1000s of apps -- you're calling your routine 2x, somehow.

(No offense, but it's your code. :)

Upvotes: 1

Benny Wong
Benny Wong

Reputation: 6851

One possible reason is that if you hooked up your button to the event, and then copied that button and hooked up the event again, you might be calling your btnSelection function twice.

In interface builder, check to see that you only have one callback to btnSelection

Upvotes: 3

Related Questions