Where there may be some countdown in the project
For example, when the mobile phone verification, the verification code will generally have a time limit, at this point in the code to enter the place you need to show a countdown
The detailed implementation is the use of iOS comes with the Nstimer
On the Code
First new
int Secondscountdown; Countdown Total time length nstimer *countdowntimer; UILabel *labeltext;
And then implement it in detail
Create UILabel Add to Current View Labeltext=[[uilabel Alloc]initwithframe:cgrectmake (ten, +, +)]; [Self.view Addsubview:labeltext]; Set Countdown total duration Secondscountdown = 60;//60 seconds Countdown //Start countdown Countdowntimer = [Nstimer Scheduledtimerwithtimeinterval:1 target:self selector: @selector (Timefiremethod) Userinfo:nil Repeats:yes]; A method is called every second after the countdown is started Timefiremethod //Set the countdown display time labeltext.text=[nsstring stringwithformat:@ "%d", Secondscountdown];
Implementing a method that runs every second
-(void) timefiremethod{ //Countdown-1 secondscountdown--; Change the countdown tag reality content labeltext.text=[nsstring stringwithformat:@ "%d", Secondscountdown]; When the countdown to 0 o'clock. Do the required operation, for example, the verification code expires cannot submit if (secondscountdown==0) { [countdowntimer invalidate]; [LabelText Removefromsuperview];} }
Have been broadly implemented, there are problems to continue to communicate
Apple Development Group: 414319235 Welcome to add welcome discussion questions
IOS Countdown Nstimer