iosiphoneuibuttonactionmethoduicontrolevents

Multiple clicks on UIButton trigger Target function multiple times


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?


Solution

  • 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;
    }