Mohammad Rabi
Mohammad Rabi

Reputation: 1412

Use performSelector:withObject: afterDelay: inside UIButton action

I am using the [performSelector:@selector(reloadData) withObject:nil afterDelay:0.01] inside IBAction of a UIButton, the reloadData method draw some subviews on the main view in a particular way, the issue is when I tap the button quickly and repeatedly the selector "ReloadData" executed multiple times, event though I am canceling the all previous requests to that selector, and this results in duplication for the subviews in the main view

-(IBAction) myButtonIsTapped
{
     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(reloadData) object:nil];

    [self performSelector:@selector(reloadData) withObject:nil afterDelay:0.01];

}

and reload data method like the following:

-(void) reloadData
{
     @synchronized(self){
        // clear all subviews from the main view
        // draw new subviews
     }
}

Upvotes: 1

Views: 1737

Answers (2)

user2064700
user2064700

Reputation: 36

Do one Thing create one BOOL variable and set in viewdidload yes and check in function if it is yes then method call and also make it no in buttonmake method.

Upvotes: 0

Rui Peres
Rui Peres

Reputation: 25917

What about this:

-(IBAction) myButtonIsTapped
{
    [self.myButton setUserInteractionEnabled:NO];

    [self performSelector:@selector(reloadData) withObject:nil afterDelay:0.01];

}

-(void) reloadData
{
    // Long task...
    // Enable the button again:
    [self.myButton setUserInteractionEnabled:YES];

}

Sometimes is just easier to control what the user is doing (UI), than logically dealing with what he has done.

Upvotes: 3

Related Questions