The app is used to calculate the remaining time from 12:00 midnight.
Current version of SDK 8.4 Xcode
Run Xcode Select Create a new Xcode project->single View application named minutestomidnight
(1) Open ViewController.h file in Xcode
(Red is the added code)
#import <UIKit/UIKit.h>
@interface viewcontroller: uiviewcontroller
{
nstimer *timer;
iboutlet UILabel *countdownlabel;
}
@property (nonatomic,retain)nstimer *timer;
@property (nonatomic,retain)iboutlet UILabel *countdownlabel;
-(void) OnTimer;
@end
(2) Open viewcontroller.m file in Xcode
#import "ViewController.h"
@interface viewcontroller ()
@end
@implementation Viewcontroller
@synthesize timer;
@synthesize Countdownlabel;
-(void) viewdidload {
[Super viewdidload];
additional setup after loading the view, typically from a nib.
[countdownlabel setFont: [uifont fontwithname:@ "Dblcdtempblack" Size :]];//font size
Countdownlabel text = @ "00:00:00" ; Initialize the value of the label
self. Timer = [nstimer scheduledtimerwithtimeinterval:1.0 target: Self selector:@selector(onTimer) userInfo:nil repeats:YES ];
/*
Scheduledtimerwithtimeinterval: (nstimeinterval) seconds
Book a timer and set a time interval. Represents a value that enters a time interval object, in seconds, of a >0 floating-point type, and if the value is <0, the system defaults to 0.1
Target: (ID) atarget
Represents the Sent object, such as self
Selector: (SEL) Aselector
Method selector, which, within the time interval, chooses to invoke an instance method
UserInfo: (ID) userInfo
This parameter can be nil, and the timer is retained and freed by the object you specify when the timer fails.
Repeats: (BOOL) Yesorno
When yes, the timer loops continuously until it expires or is released, and when no, the timer is sent out once and then fails.
*/
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
//Dispose of any resources, can be recreated.
}
-(void) OnTimer
{
nsdate *now = [nsdate date];
Nscalendar *gregorian = [[nscalendar alloc]initwithcalendaridentifier: Nscalendaridentifiergregorian];
/* nsdate--Represents an absolute point in time
nstimezone--time zone information
& nbsp nslocale--Localization information
nsdatecomponents--a class that encapsulates a specific date, time, week, quarter, and so on
nscalendar--Calendar class, which provides most of the date compute interfaces and allows you to convert between NSDate and nsdatecomponents
NSDateFormatter--Used to convert between dates and strings
Nscalendar Calendar class ****//
The following methods are available for creating or initializing
+ (ID) Currentcalendar;
Get the current user's logical calendar (logical calendars)
+ (ID) Autoupdatingcurrentcalendar;
Get the current user's logical calendar (logical calendar), ...
-(ID) Initwithcalendaridentifier: (NSString *) identifier;
Initialize to various calendars. The range of identifier can be:
Nscalendaridentifiergregorian Solar Calendar
nscalendaridentifierbuddhist Buddhist Calendar
Nscalendaridentifierchinese China calendar
nscalendaridentifierhebrew Hebrew calendar
nscalendaridentifierislamic Islamic Calendar
nscalendaridentifierislamiccivil Islamic Civil calendar
Nscalendaridentifierjapanese Japan Calendar
*/
nsdatecomponents *datecomponents = [Gregorian components:(kcfcalendarunithour| Kcfcalendarunitminute| Kcfcalendarunitsecond) fromdate: now];
Nsinteger hour = [datecomponents hour];
nsinteger min = [datecomponents minute];
Nsinteger sec = [datecomponents second];
SEC = -sec;
min = -min;
hour = hour;
Countdownlabel. Text = [nsstring stringwithformat:@ "%ld:%ld:%ld", hour,min,sec];
}
@end
(3) UIView interface settings
-Black background
Click Main.storyboard
Select View Controller->view
Set the background to black
Select Attributes Inspector->background->black Color
(4) Add UILabel display countdown time
-Red Font
Select: Tools--Library; Drag a Label from the Library display menu to Main View
Color and font size of fonts from label Attributes
(5) write to the UILabel class file
Right-click the Label control and move the mouse over the circle behind "New referencing Outlet"; The circle becomes (+); Drag to a straight line to connect to the "view Controller";
Release the mouse Select button appears "Countdownlabel"; Pick it up.
Select: File--Save
Finally, in XCode, choose Build and then Running
(6) Simulator
This article originates from the online a blog tutorial, after I revise and test. Original Blog Address http://blog.sina.com.cn/s/blog_5fae23350100djg2.html
One, midnight Countdown "Getting Started with Apple iOS instance programming tutorial"