简体   繁体   中英

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

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.

做一个Thing创建一个BOOL variable并在viewdidload设置yes并检查函数是否为yes然后是方法调用,并且在buttonmake方法中也没有。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM