In the home company to do the project, did a 60 seconds to obtain the function of the verification code, at that time did a timer, button trigger timer, logic is no problem, but the actual operation, the malicious in a short period of time to click on the button to get the verification code, the button click event was called several times, The timer is also called several times, the event of one second minus one becomes less than one second, and the method continues to be called after reducing to 0, until the response hits are called, which is completely incompatible with our mind. After February, I work in the new company, looking back to this problem, in order to solve this problem, I found in Baidu on a lot of information, solve the solution to write their own blog, and everyone to share their own technology, but also for their own have always wanted to write a blog to start, record development problems encountered, slowly grow.
All right, let's go. Topic: 1. A 60-second function to get the verification code, first write the button, in the method of triggering the button to do the following:
-(void) Getbtnaction: (UIButton *) btn{//Get Verification Code button click
Multiple clicks in 0.2-second intervals respond to only one click event, and todosomething is exactly what we want to achieve.
[Self class] cancelpreviousperformrequestswithtarget:self selector: @selector (todosomething:) object:btn]; [Self performselector: @selector (todosomething:) withobject:btn afterdelay:0.2f];}
-(void) todosomething: (UIButton *) btn{
It's a way to write Timer,timer to tune himself.
}
2. It's over here, and the key is that step:
[Self class] cancelpreviousperformrequestswithtarget:self selector: @selector (todosomething:) object:btn]; [Self performselector: @selector (todosomething:) withobject:btn afterdelay:0.2f];
We hope to help you.
iOS fix button multiple clicks to trigger only one event method in a short time