一、午夜倒数《苹果iOS实例编程入门教程》
该app为应用的功能为计算离午夜12:00点的剩余时间
现版本 SDK 8.4 Xcode
运行Xcode 选择 Create a new Xcode project ->Single View Application 命名 minutesToMidnight
(1) 在xCode打开 ViewController.h 文件
(红色为所添加的代码)
#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) 在xCode打开 ViewController.m 文件
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize timer;
@synthesize countdownLabel;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[countdownLabel setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:33]];// 字体 大小
countdownLabel.text = @"00:00:00";// 初始化Label的值
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
/*
scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
预订一个Timer,设置一个时间间隔。表示输入一个时间间隔对象,以秒为单位,一个>0的浮点类型的值,如果该值<0,系统会默认为0.1
target:(id)aTarget
表示发送的对象,如self
selector:(SEL)aSelector
方法选择器,在时间间隔内,选择调用一个实例方法
userInfo:(id)userInfo
此参数可以为nil,当定时器失效时,由你指定的对象保留和释放该定时器。
repeats:(BOOL)yesOrNo
当YES时,定时器会不断循环直至失效或被释放,当NO时,定时器会循环发送一次就失效。
*/
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)onTimer
{
NSDate *now = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
/* NSDate -- 表示一个绝对的时间点
NSTimeZone -- 时区信息
NSLocale -- 本地化信息
NSDateComponents -- 一个封装了具体年月日、时秒分、周、季度等的类
NSCalendar -- 日历类,它提供了大部分的日期计算接口,并且允许您在NSDate和NSDateComponents之间转换
NSDateFormatter -- 用来在日期和字符串之间转换
//****NSCalendar 日历类****//
创建或初始化可用以下方法
+ (id)currentCalendar;
取得当前用户的逻辑日历(logical calendar)
+ (id)autoupdatingCurrentCalendar;
取得当前用户的逻辑日历(logical calendar), ......
- (id)initWithCalendarIdentifier:(NSString *)identifier;
初始化为各种日历。identifier的范围可以是:
NSCalendarIdentifierGregorian 阳历
NSCalendarIdentifierBuddhist 佛历
NSCalendarIdentifierChinese 中国日历
NSCalendarIdentifierHebrew 希伯来日历
NSCalendarIdentifierIslamic 伊斯兰日历
NSCalendarIdentifierIslamicCivil 伊斯兰民事日历
NSCalendarIdentifierJapanese 日本日历
*/
NSDateComponents *dateComponents = [gregorian components:(kCFCalendarUnitHour|kCFCalendarUnitMinute|kCFCalendarUnitSecond) fromDate:now];
NSInteger hour = [dateComponents hour];
NSInteger min = [dateComponents minute];
NSInteger sec = [dateComponents second];
sec = 60 -sec;
min = 59-min;
hour = 23 - hour;
countdownLabel.text = [NSString stringWithFormat:@"%ld:%ld:%ld",hour,min,sec];
}
@end
(3) UIView 界面设置
- 黑色背景
点击Main.storyboard
选择view controller ->view
将背景设为黑色
选择Attributes Inspector->Background->Black Color
(4) 加入 UILabel 显示倒数时间
- 红色字体
选择: Tools -> Library ; 从Library显示菜单中拖拉一个 Label 到 Main View
从Label Attributes 上选着字体的颜色和字体大小
(5)写入 UILabel 的 class file
鼠标右击Label控件,鼠标移动到"New Referencing Outlet" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"view controller";
放开鼠标选择键出现 "countdownLabel"; 选上它。
选择: File -> Save
最后在 xCode 选择 Build and then Running
(6)模拟器效果图

本文源于网上一博客教程,经过本人修改和测试。原blog地址 http://blog.sina.com.cn/s/blog_5fae23350100djg2.html
一、午夜倒数《苹果iOS实例编程入门教程》的更多相关文章
- 七、考反映小游戏《苹果iOS实例编程入门教程》
该app为应用的功能为一个简单的考反应游戏 纲要:-UIButton, UILabel, UIImageView 的运用:-利用rendom增加游戏可玩性: 游戏说明: 在按下开始游戏后,分为三盏的指 ...
- 六、雪花《苹果iOS实例编程入门教程》
该app为应用的功能为制作一场雪景 现版本 SDK 8.4 Xcode 纲要:- UIImageView 的运用- onTimer 代码运用- onAnimation 代码运用 运行Xcode 选择 ...
- 五、点数器《苹果iOS实例编程入门教程》
该app为应用的功能为一个简单的数数程序 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View Applic ...
- 四、卫星定位《苹果iOS实例编程入门教程》
该app为应用的功能为用iPhone 显示你现在的位置 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View ...
- 三、图像移动《苹果iOS实例编程入门教程》
该app为应用的功能为动态移动动画 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View Applicati ...
- 二 、打开地图《苹果iOS实例编程入门教程》
该app为应用的功能为给你的iPhone打开google地图有效地址连接 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Si ...
- 【C语言C++编程学习笔记】基础语法,第一个简单的实例编程入门教程!
C语言/C++编程学习:一个简单的实例 让我们来看一个简单的C语言程序.从下面的程序可以看出编写C语言程序的一些基本特征. 如果你能知道该程序将会在显示器上显示一些内容,那说明你还是知道一些的! ...
- PHP面向对象(OOP)编程入门教程
面向对象编程(OOP)是我们编程的一项基本技能,PHP5对OOP提供了良好的支持.如何使用OOP的思想来进行PHP的高级编程,对于提高 PHP编程能力和规划好Web开发构架都是非常有意义的.下面我们就 ...
- VS2010/MFC编程入门教程之目录和总结
鸡啄米的这套VS2010/MFC编程入门教程到此就全部完成了,虽然有些内容还未涉及到,但帮助大家进行VS2010/MFC的入门学习业已足够.以此教程的知识为基础,学习VS2010/MFC较为深入的内容 ...
随机推荐
- OS X thrift setup
OS X Setup The following command install all the required tools and libraries to build and install t ...
- hdu 4050 2011北京赛区网络赛K 概率dp ***
题目:给出1-n连续的方格,从0开始,每一个格子有4个状态,左右脚交替,向右跳,而且每一步的步长必须在给定的区间之内.当跳出n个格子或者没有格子可以跳的时候就结束了,求出游戏的期望步数 0:表示不能到 ...
- VS2010 打开 VS2012 的项目
用 VS2010 打开 VS2012 项目,只需两步. 1. 修改解决方案文件(*.sln) 使用记事本打开 *.sln 文件,将里面的 Microsoft Visual Studio Solutio ...
- Error: Could not find or load main class test.EditFile
今天写了一个简单的小程序,运行之后发现Error: Could not find or load main class test.EditFile,项目无法启动.删除main中的所有内容之后依旧提示该 ...
- 【转】GeoHash核心原理解析
好久没更新过博客了,先转载一篇文章吧. 源地址:http://www.cnblogs.com/LBSer/p/3310455.html 引子 机机是个好动又好学的孩子,平日里就喜欢拿着手机地图点点按按 ...
- System.Web.Caching.Cache类 缓存
1.文件缓存依赖 public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender ...
- 用js完成毫秒格式数据的日期格式化任务
后台传过来的数据 creationTime 在后台是Date类型的 毫秒转换成 05-24 月 日格式的 //获得月日得到日期oTime function getMoth(str){ var ...
- 分享Kali Linux 2016.2第48周镜像文件
分享Kali Linux 2016.2第48周镜像文件Kali Linux官方于11月27日发布Kali Linux 2016.2的第48周镜像.这次延续以往规律,仍然是11个镜像文件.默认的Gnom ...
- MVC 异常处理机制
方法一 :web.config配置文件的 system.web 接点下添加,若为On则不会将异常信息反馈到用户,而是友好的跳转到error.htm <customErrors mode=&quo ...
- 【转】浅谈 C++ 中的 new/delete 和 new[]/delete[]
在 C++ 中,你也许经常使用 new 和 delete 来动态申请和释放内存,但你可曾想过以下问题呢? new 和 delete 是函数吗? new [] 和 delete [] 又是什么?什么时候 ...