10 Seconds Countdown
#import "HMViewController.h"@interfaceHmviewcontroller () <UIAlertViewDelegate>@property (Weak, nonatomic) Iboutlet UILabel*Counterlabel, @property (nonatomic, strong) Nstimer*timer;@end@implementationHmviewcontroller/** Start*/-(ibaction) start{//countdown 10 seconds, update the label display every second//Timer /** Parameter Description 1. Time interval, double 2. Listens for clock-triggered objects 3. Call Method 4. UserInfo, which can be any object, usually passing nil 5. Repeats: Repeat /c7>*/Self.counterLabel.text=@"Ten"; //The Scheduledtimerwithtimeinterval method essentially creates a clock,//the mode to be added to the run loop is Defaultrunloopmode// ---------------------------------------------- //1>//Self.timer = [Nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (updatetimer:) userinfo:@ "Hello timer" repeats:yes]; // ---------------------------------------------- //2> and 1 equivalence//Self.timer = [Nstimer timerwithtimeinterval:1.0 target:self selector: @selector (updatetimer:) Userinfo:nil Repeats : YES];// //Add a timer to the run loop// //mode: Default run-cycle mode//[[Nsrunloop Currentrunloop] AddTimer:self.timer Formode:nsdefaultrunloopmode]; // ---------------------------------------------- //3>Self.timer = [Nstimer timerwithtimeinterval:1.0target:self selector: @selector (updatetimer:) Userinfo:nil Repeats:yes]; //Add a timer to the run loop//mode: Nsrunloopcommonmodes running cycle mode (monitor scrolling mode)[[Nsrunloop Currentrunloop] AddTimer:self.timer formode:nsrunloopcommonmodes]; //[self updateTimer:self.timer];}/** Clock Update method*/- (void) Updatetimer: (Nstimer *) timer{//NSLog (@ "%s", __func__); //1. Remove the number from the label intCounter =Self.counterLabel.text.intValue; //2. Determine if zero, if 0, stop clock if(--counter <0) { //Stop Clock[self pause]; //Prompt user, prompt box//Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Start" message:@ "Start ..." delegate:self Cancelbuttont itle:@ "Cancel" otherbuttontitles:@ "OK", @ "haha", nil];////[alert show];[[[Uialertview alloc] Initwithtitle:@"Start"Message@"Here we go ." Delegate: Self Cancelbuttontitle:@"Cancel"Otherbuttontitles:@"Determine",@"haha", nil] show]; } Else { //CTRL + I//3. Modify the numbers and update the UISelf.counterLabel.text = [NSString stringWithFormat:@"%d", counter]; }}/** Pause*/-(ibaction) pause{//stop Clock, invalidate is the only way to stop the clock//once the Invalidate method is called, the timer is invalid, and if you start the clock again, you need to re-instantiate[Self.timer invalidate];}#pragmaMark-alertview Proxy Method-(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (nsinteger) buttonindex{NSLog (@"%ldaaa-------", (Long) buttonindex);}@end
iOS Sixth day (2:10 seconds Countdown)