IOS (10) UI _ NSTimer_ios timer, ios_nstimer_ios
CAT/CAT sharing, must be excellent
Material address: http://blog.csdn.net/u013357243/article/details/44627787
Original address: http://blog.csdn.net/u013357243? Viewmode = contents
First view results
Code
Original address: http://blog.csdn.net/u013357243? Viewmode = contents // NYViewController. m // 05-Countdown /// Created by apple on 15-3-25. // Copyright (c) 2015 znycat. all rights reserved. // # import "NYViewController. h "@ interface NYViewController () <UIAlertViewDelegate> @ property (weak, nonatomic) IBOutlet UILabel * counterLabel; @ property (nonatomic, strong) NSTimer * timer; @ end @ implementation NYViewController/** start */-(IBAction) start {// countdown 10 seconds, Timer/* NST Description of the parameter scheduledTimerWithTimeInterval: 1, interval, double 2, listener clock-triggered object 3, call Method 4, userInfo, which can be any object. Generally, nil is passed. If there is a pass value, most of them are used to identify which 5 is used in multiple counters. repeats: whether to repeatedly execute the call method. * // The scheduledTimerWithTimeInterval method is essentially to create a clock. // The mode added to the running cycle is DefaultRunloopMode // _________________________________________________________________________________________ // 1>/self. timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @ selector (updateTimer) userInfo: nil repeats: YES]; // 2> same as 1 // self. timer = [nst1_timerwithtimeinterval: 1.0 target: self selector: @ selector (updateT Optional) userInfo: nil repeats: YES]; // Add timer to the running cycle. Mode: Default running cycle mode // [[[nsunloop currentRunLoop] addTimer: self. timer forMode: NSDefaultRunLoopMode]; // ______________________________________________________________________________________ // 3> self. timer = [NSTimer timerWithTimeInterval: 1.0 target: self selector: @ selector (updateTimer) userInfo: nil repeats: YES]; // Add the timer to the running cycle. Mode: nsunloopcommonmodes listens to the rolling mode [[ Nsunloop currentRunLoop] addTimer: self. timer forMode: nsunloopcommonmodes];}/** update the counterLabel attribute per second */-(void) updateTimer {/1. Obtain the number int counter = self in the tag. counterLabel. text. intValue; // 2. Determine whether the value is 0. if yes, stop the clock if (-- counter <0) {// prompt the user. The prompt box is [[[UIAlertView alloc] initWithTitle: @ "start" message: @ "start... "Delegate: self cancelButtonTitle: @" cancel "otherButtonTitles: @" OK ", nil] show]; // The last input in AlertView is an array, you can use the proxy method to implement [self pause];} else {//, 3. Modify the number and display the updated UILabel self. counterLabel. text = [NSString stringWithFormat: @ "% d", counter] ;}/ ** pause */-(IBAction) pause {// stop the clock. invalidate is the only method, timer is killed in one call. If you want to use it again, you can only instantiate [self. timer invalidate];}-(IBAction) stop {[self pause]; self. counterLabel. text = @ "10" ;}# pragma mark-alertView proxy method-(void) alertView :( UIAlertView *) alertView didDismissWithButtonIndex :( NSInteger) buttonIndex {NSLog (@ "% d ", buttonIndex);} @ end
Note: NSTimer
Usage:
Self. timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @ selector (updateTimer) userInfo: nil repeats: YES]
Parameter description:
1, interval, double
2. Listen to the object triggered by the clock
3. Call Method
4. userInfo can be any object. nil is usually passed. If a value is passed, most of them are used to identify which one to use in multiple counters.
5. repeats: whether to repeatedly execute the call method.
Whether to continue the timer when a rolling event occurs
Add timer to the running cycle. Mode: nsunloopcommonmodes listens to the rolling mode.
[[Nsunloop currentRunLoop] addTimer: self. timer forMode: NSDefaultRunLoopMode];
Prompt box UIAlertView
Prompt box
[[UIAlertView alloc] initWithTitle: @ "Start" message: @ "start ..." Delegate: self cancelButtonTitle: @ "cancel" otherButtonTitles: @ "OK", nil] show];
The last input in AlertView is an array, which can be implemented through proxy.
# Pragma mark-alertView proxy method-(void) alertView :( UIAlertView *) alertView didDismissWithButtonIndex :( NSInteger) buttonIndex {NSLog (@ "% d", buttonIndex ); // 0 indicates the cancel button. // you can add the button if to determine the number of buttonIndx events}
Ps: Create an iOS communication learning group: 304570962
You can add a cat QQ: 1764541256 or znycat
Let's study hard together.
Http://blog.csdn.net/u013357243? Viewmode = contents