Multiple clicks on UIButton trigger Target function multiple times

2.4k views Asked by At

I have a UIButton. I bound a target as follows.

[button addTarget:self action:@selector(myFunction) 
                forControlEvents:UIControlEventTouchUpInside];

When i click my Button multiple times quickly it invoke the target function multiple times. On Tapping button i present a new View controller. when i click 3 times quickly then my new view controller is shown 3 times.

This is something stupid. Whats the point of triggering the function again once the View has been shifted to a new View controller. Why the Hell Apple do such stupid things ?

Any Help please?

4

There are 4 answers

0
Dharmbir Singh On BEST ANSWER

First of all its not apple bugs. It should be handle manually. So follow these step

First make your global instance of your button then do this

.h file

@property (weak, nonatomic) IBOutlet UIButton *btn;

.m file

- (IBAction)myFunction:(id)sender
{
    self.btn.userInteractionEnabled = NO;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.btn.userInteractionEnabled = YES;
}
0
Pravin Tate On

Take one global bool flag like "isItDone" or it declare in singleton class.

  1. in "myFunction" set it as false
  2. which View controller you push on that function in that class's "ViewDidAppear" method set as true.

it will help you. I have same problem and it is good solution for that to manage it using one global variable.

5
Mahesh Agrawal On

I think this will help you.

Change your calling function like this

- (IBAction)myFunction:(id)sender
{
    UIButton *button = (UIButton*)sender;
    button.userInteractionEnabled = NO;
}

and call your function like this

[button addTarget:self action:@selector(myFunction:) 
            forControlEvents:UIControlEventTouchUpInside];

if you want to store the selection incase you came back to the view controller then only you need to keep a boolean flag to store if its clicked once or not.

2
Paddy On

Set the IBOutlet to your button, in the viewWillAppear method write,

button.userInteractionEnabled = YES;

and when you click on the button set,

button.userInteractionEnabled = NO;