iOS fix button multiple clicks to trigger only one event method in a short time

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.