Set an NSTtimer to display the current time

Source: Internet
Author: User

Sets an NSTtimer to display the current time,

Define NSTimer usage: Time Interval NSTimeInterval floating point type, event proxy
Delegate and event processing method @ selector ();
+ (NSTimer *) scheduledTimerWithTimeInterval :( NSTimeInterval) ti target :( id) aTarget selector :( SEL) aSelector userInfo :( id) userInfo repeats :( BOOL) yesOrNo; to initialize a time Timer

Nstcoa is a common Timer class in Cocoa. The basic operations are as follows:
The handleTimer method can be customized. You can create a timer as needed, and handleTimer can be executed every 1.0 seconds.

 


First, obtain the Time of the specified city.


NSString * tmpTimeStr;

NSDate * nowdate = [[NSDate alloc] init];

NSDateFormatter * formatter = [[NSDateFormatter alloc] init];

NSTimeZone * timeZone = [NSTimeZone timeZoneWithName: @ "Asia/Shanghai"];

[Formatter setTimeZone: timeZone];

[Formatter setDateFormat: @ "YYYY-MM-dd-HH-mm-ss"];

NSString * timeStr = [formatter stringFromDate: nowdate];

In this way, the time format is NSString, and NSTimer is set as follows:

NSTimeInterval timeinteger = 1.0;

// Timer

Nstmer * timer;

Timer = [NSTimer scheduledTimerWithTimeInterval: timeInterval

Target: self

Selector: @ selector (showNowTime :)

UserInfo: nil

Repeats: YES];

Set the current time for each second. In this way, the use of an NSTimer is realized.

 

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.