2

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?

Mahesh Agrawal
  • 3,348
  • 20
  • 34
waseemwk
  • 1,499
  • 3
  • 15
  • 44

4 Answers4

3

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;
}
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
2

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.

Pravin Tate
  • 1,145
  • 8
  • 18
1

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.

Mahesh Agrawal
  • 3,348
  • 20
  • 34
  • I think but using that code user can click ones on that button only, am i right or I miss understand something? – Pravin Tate Jun 22 '15 at 05:38
  • No yo right. according to my answer user can click on the button only once and if you want the user click again the button in any condition then you can just enable the user interaction by checking. – Mahesh Agrawal Jun 22 '15 at 05:39
  • make sure you are calling the function like (myFunction:) not only (myFunction) – Mahesh Agrawal Jun 22 '15 at 05:40
  • so i think better way we can manage it using bool value and it is useful for all situation like this. what you think – Pravin Tate Jun 22 '15 at 05:46
  • your answer is correct for a single button but if there are lots of buttons and all needs to be navigate to a single viewController with some logic there with button then my answer will be applicable. Am telling this because i ran into some this kind of problem am explaining. Thanks. – Mahesh Agrawal Jun 22 '15 at 08:21
1

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;
Paddy
  • 766
  • 5
  • 16