实例iPhone编程入门教程-第一天
2011-09-22 14:03
197 查看
DAY ONE-MinutesToMidnight
大家一起来建立我们的第一个iPhone app,给你的iPhone计算离午夜12:00点的剩余时间。
现在已经改为:sdk4.2版本。
一切版权归http://blog.sina.com.cn/iphonesdk 所有。
首先运行以安装好的xCode
选择: File->New Project.
从 "New Project" 窗口
选择 : iPhone OS ->Applications->View-Based Application
命名 : 我这里命名为 "minutesToMidnight"
(1) 在xCode打开minutesToMidnightViewController.h文件
(红色为所添加的代码)
#import<UIKit/UIKit.h>
@interface minutesToMidnightViewController : UIViewController{
NSTimer *timer;
IBOutletUILabel *countdownLabel;
}
@property(nonatomic,retain) UILabel *countdownLabel;
@property (nonatomic, retain)NSTimer *timer;
-(void)onTimer;
@end
(2) 在xCode打开minutesToMidnightViewController.m 文件
#import "minutesToMidnightViewController.h"
@implementation minutesToMidnightViewController
@synthesize timer;
@synthesize countdownLabel;
// Implement viewDidLoad to do additional setup afterloading the view, typically from a nib.
- (void)viewDidLoad {
[superviewDidLoad];
[countdownLabel setFont:[UIFontfontWithName:@"DBLCDTempBlack" size:128.0]];
countdownLabel.text = @"00:00:00";
self.timer = [NSTimerscheduledTimerWithTimeInterval:1.0 target:selfselector:@selector(onTimer) userInfo:nil repeats:YES];
}
-(void)onTimer{
NSDate* now =[NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregoriancomponents:(NSHourCalendarUnit |NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now];//不换行
NSIntegerhour = [dateComponents hour];
NSIntegermin = [dateComponents minute];
NSIntegersec = [dateComponents second];
sec = 60 - sec;
min = 59 - min;
hour = 23 - hour;
[gregorian release];
countdownLabel.text = [NSString stringWithFormat:@"%d:%d:%d", hour,min,sec];
}
- (void)didReceiveMemoryWarning {
// Releasesthe view if it doesn't have a superview.
[superdidReceiveMemoryWarning];
// Releaseany cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Releaseany retained subviews of the main view.
// e.g.self.myOutlet = nil;
}
- (void)dealloc {
[superdealloc];
[timerrelease];
[countdownLabel release];
}
@end
完成后打开Interface Builder,进行UIView设置
(3) UIView 界面设置
-黑色背景,红色字体,让它看起来像一个闹钟。
双击文件: "MinutesToMidnightViewController.xib" ;
然后 "Interface Builder" 会自动打开,在这里我们可以编辑改变界面
选择: Tools -> Reveal In Document Window-> View
选择: Tools -> AttributesInspector
在色条内选择 "黑色",可以看到背景变为黑色
(4) 加入 UILabel显示我们的倒数时间
选择: Tools -> Library ; 从Library显示菜单中拖拉一个Label 到 Main View
在主显示上点击 Label;从Label Attributes 上选着字体的颜色和字体大小。
(5)从Interface Builder 写入 UILabel 的 class file
再打开SDK工具 InterfaceBuilder
在主视窗口或文件窗口;点击 Label
选择: Tools -> Connection Inspector
移动鼠标在"New Referencing Outlet" 后面圆圈上; 圆圈变为(+);拖向直线连接到"File's Owner";
放开鼠标选择键出现 "countdownLabel"; 选上它。
选择: File -> Save then close InterfaceBuilde
最后在 xCode 选择 Build->Build and Go.
下载今天程序文件:
MinsToMidnight.zip
大家一起来建立我们的第一个iPhone app,给你的iPhone计算离午夜12:00点的剩余时间。
现在已经改为:sdk4.2版本。
一切版权归http://blog.sina.com.cn/iphonesdk 所有。
首先运行以安装好的xCode
选择: File->New Project.
从 "New Project" 窗口
选择 : iPhone OS ->Applications->View-Based Application
命名 : 我这里命名为 "minutesToMidnight"
(1) 在xCode打开minutesToMidnightViewController.h文件
(红色为所添加的代码)
#import<UIKit/UIKit.h>
@interface minutesToMidnightViewController : UIViewController{
NSTimer *timer;
IBOutletUILabel *countdownLabel;
}
@property(nonatomic,retain) UILabel *countdownLabel;
@property (nonatomic, retain)NSTimer *timer;
-(void)onTimer;
@end
(2) 在xCode打开minutesToMidnightViewController.m 文件
#import "minutesToMidnightViewController.h"
@implementation minutesToMidnightViewController
@synthesize timer;
@synthesize countdownLabel;
// Implement viewDidLoad to do additional setup afterloading the view, typically from a nib.
- (void)viewDidLoad {
[superviewDidLoad];
[countdownLabel setFont:[UIFontfontWithName:@"DBLCDTempBlack" size:128.0]];
countdownLabel.text = @"00:00:00";
self.timer = [NSTimerscheduledTimerWithTimeInterval:1.0 target:selfselector:@selector(onTimer) userInfo:nil repeats:YES];
}
-(void)onTimer{
NSDate* now =[NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregoriancomponents:(NSHourCalendarUnit |NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now];//不换行
NSIntegerhour = [dateComponents hour];
NSIntegermin = [dateComponents minute];
NSIntegersec = [dateComponents second];
sec = 60 - sec;
min = 59 - min;
hour = 23 - hour;
[gregorian release];
countdownLabel.text = [NSString stringWithFormat:@"%d:%d:%d", hour,min,sec];
}
- (void)didReceiveMemoryWarning {
// Releasesthe view if it doesn't have a superview.
[superdidReceiveMemoryWarning];
// Releaseany cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Releaseany retained subviews of the main view.
// e.g.self.myOutlet = nil;
}
- (void)dealloc {
[superdealloc];
[timerrelease];
[countdownLabel release];
}
@end
完成后打开Interface Builder,进行UIView设置
(3) UIView 界面设置
-黑色背景,红色字体,让它看起来像一个闹钟。
双击文件: "MinutesToMidnightViewController.xib" ;
然后 "Interface Builder" 会自动打开,在这里我们可以编辑改变界面
选择: Tools -> Reveal In Document Window-> View
选择: Tools -> AttributesInspector
在色条内选择 "黑色",可以看到背景变为黑色
(4) 加入 UILabel显示我们的倒数时间
选择: Tools -> Library ; 从Library显示菜单中拖拉一个Label 到 Main View
在主显示上点击 Label;从Label Attributes 上选着字体的颜色和字体大小。
(5)从Interface Builder 写入 UILabel 的 class file
再打开SDK工具 InterfaceBuilder
在主视窗口或文件窗口;点击 Label
选择: Tools -> Connection Inspector
移动鼠标在"New Referencing Outlet" 后面圆圈上; 圆圈变为(+);拖向直线连接到"File's Owner";
放开鼠标选择键出现 "countdownLabel"; 选上它。
选择: File -> Save then close InterfaceBuilde
最后在 xCode 选择 Build->Build and Go.
下载今天程序文件:
MinsToMidnight.zip
相关文章推荐
- 实例iPhone编程入门教程-第十一天
- 实例iPhone编程入门教程-第十二天
- 实例iPhone编程入门教程-第十三天
- 实例iPhone编程入门教程-第二天
- 实例iPhone编程入门教程-第三天
- 实例iPhone编程入门教程-第四天
- 实例iPhone编程入门教程-第五天
- 实例iPhone编程入门教程-第六天
- 实例iPhone编程入门教程-第七天
- 实例iPhone编程入门教程-第八天
- 实例iPhone编程入门教程-第九天
- 实例iPhone编程入门教程-第十天
- Expression Blend实例中文教程(9) - 行为快速入门Behaviors
- WebService入门实例项目详细教程(图)
- 微信公众平台开发教程(四) 实例入门:机器人(附源码)
- jQuery中文入门指南,翻译加实例,jQuery的起点教程
- MongoDB入门教程之C#驱动操作实例
- React 入门实例教程
- 微信公众平台开发教程(四) 实例入门:机器人(附源码)
- Expression Blend实例中文教程(13) - 控件模板快速入门ControlTemplates