第一天 午夜倒数《苹果iOS实…

本文详细介绍如何使用Xcode和iOS SDK创建一个简单的午夜倒数应用,该应用能够显示距离午夜的时间,并通过定时器实时更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DAY ONE - MinutesToMidnight

大家一起来建立我们的第一个 iPhone app,给你的iPhone计算离午夜12:00点的剩余时间。[转载]第一天 <wbr>午夜倒数《苹果iOS实例编程入门教程》

 

现在已经改为:sdk4.2版本。


首先运行以安装好的 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;
   
    IBOutlet UILabel *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 after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    [countdownLabel setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:128.0]];
     countdownLabel.text = @"00:00:00";
     self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(onTimer) userInfo:nil repeats: YES];
}

-(void)onTimer{
    NSDate* now = [NSDate date];

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit  | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now]; //不换行
    NSInteger hour = [dateComponents hour];
    NSInteger min = [dateComponents minute];
    NSInteger sec = [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 {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
   
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
    [timer release];
    [countdownLabel release];
}

@end
 

完成后打开Interface Builder,进行UIView设置


(3) UIView 界面设置
- 黑色背景,红色字体,让它看起来像一个闹钟。

双击文件: "MinutesToMidnightViewController.xib" ;
然后 "Interface Builder"  会自动打开,在这里我们可以编辑改变界面
 
选择: Tools -> Reveal In Document Window -> View

选择:  Tools -> Attributes Inspector

在色条内选择 "黑色",可以看到背景变为黑色

(4) 加入 UILabel 显示我们的倒数时间

选择: Tools -> Library ; 从Library显示菜单中拖拉一个 Label 到 Main View
在主显示上点击 Label;从Label Attributes 上选着字体的颜色和字体大小。


(5)从 Interface Builder 写入 UILabel 的 class file


再打开SDK工具 Interface Builder

在主视窗口或文件窗口;点击 Label

选择: Tools -> Connection Inspector

移动鼠标在"New Referencing Outlet" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"File's Owner";
放开鼠标选择键出现 "countdownLabel"; 选上它。

选择: File -> Save then close Interface Builde 


最后在 xCode 选择 Build->Build and Go

下载今天程序文件: Day01.zip

 

文章本人经过修改及测试,希望大家多多交流。欢迎转载,但请注明出处,多珍惜别人的劳动成果,谢谢大家。

一切版权归http://blog.sina.com.cn/iphonesdk 所有。

更多关于iPhone编程的内容可以参阅下书,
链接:
欢迎来到 <wbr><wbr>iPhone <wbr><wbr>SDK <wbr><wbr>编程语言 <wbr><wbr>@ <wbr><wbr>sina <wbr><wbr>Blog
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值