IOS (10) UI _ NSTimer_ios timer, ios_nstimer_ios

Source: Internet
Author: User

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

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.