Objective-C Question

Prevent multiple Tap on the same UIButton

Some times in my app I get this error because the UI freezes and the users tap more than once the buttons:

"pushing the same view controller instance more than once is not

I have tried this:

How do I prevent multiple event on same UIButton in iOS?

And it works like a charm but if my tabbar has more than 5 elements if I tab the button that shows an element greater than 5 the more button animates from left to right.

Is there other way to prevent the double tab in an easy way that does not use animations?.

This is the code I'm using:

- (IBAction)btnAction:(id)sender {
UIButton *bCustom = (UIButton *)sender;
bCustom.userInteractionEnabled = NO;
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
[self selectTabControllerIndex:bCustom.tag];
} completion:^(BOOL finished){
bCustom.userInteractionEnabled = YES;

Answer Source

First a tip, if you only have button's calling that selector, you can change the id to UIButton* and drop the extra variable bCustom.

Now, to solve your issue, you just need to ensure you turn userInteractionEnabled back to YES after you'd done whatever else you needed to do. Using the animation block is just an easy way because it has a completion handler built in.

You can do this simply by having selectTabControllerIndex method do the work for you.

Something like this:

- (IBAction)btnAction:(UIButton*)sender {
    sender.userInteractionEnabled = NO;
    [self selectTabControllerForButton:sender];

- (void)selectTabControllerForButton:(UIButton*)sender {
    // Whatever selectTabControllerIndex does now goes here, use sender.tag as you used index
    sender.userInteractionEnabled = YES;

If you possibly had other code you needed to execute afterwards, you could add a completion handler to your selectTabControllerIndex method instead and then call the completion handler. Inside that you'd include the sender.userInteractionEnabled = YES; line. But if it's always the same code, the first way is easier and faster.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download