IOS countdown NSTimer

Source: Internet
Author: User

IOS countdown NSTimer

There may be some countdown points in the project

For example, when mobile phone verification is performed, the Verification Code generally has a time limit. At this time, a countdown is required for entering the verification code.

 

The specific implementation method is to use NSTimer that comes with iOS

 

Code on

Create

 

 

Int secondsCountDown; // total countdown duration NSTimer * countDownTimer; UILabel * labelText;


 

Then implement

 

 

// Create a UILabel and add it to the current view labelText = [[UILabel alloc] initWithFrame: CGRectMake (10,120,120, 36)]; [self. view addSubview: labelText]; // sets the total countdown duration secondsCountDown = 60; // 60 seconds countdown // starts the countdown countDownTimer = [nst1_scheduledtimerwithtimeinterval: 1 target: self selector: @ selector (timeFireMethod) userInfo: nil repeats: YES]; // After the start countdown, the method timeFireMethod is called every second. // you can specify the time when the countdown is displayed as labelText. text = [NSString stringWithFormat: @ % d, secondsCountDown];

Implementation Method per second

 

 

-(Void) timeFireMethod {// countdown-1 secondsCountDown --; // modify the actual content labelText of the Countdown tag. text = [NSString stringWithFormat: @ % d, secondsCountDown]; // The operation required when the countdown reaches 0. For example, if (secondsCountDown = 0) cannot be submitted when the verification code expires) {[countDownTimer invalidate]; [labelText removeFromSuperview];}


 

It has been roughly implemented. If you have any questions, continue to communicate.

 

 

Related Article

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.