iphone学习之旅之实例:LED电子时钟

在我们的iphone上如果有一个LED显示的电子时钟会有一种特别的感觉吧,呵呵。

 

首先,我们打开Xcode,点击FileNew Project,选择iPhone OSApplication,在这里我们选择View-based Application模版(我们的整个应用程序只有一个视图),点选Choose之后保存为LEDClick工程(默认整个工程会保存在/Users/当前登陆用户名/Documents下面)。之后点击OK就创建了了整个目录。

我们来看Groups&Files窗体,它分类显示了项目中的所有的信息。下面我们来进行具体的程序编写。对于我们来说,整个程序只有一个输出口(IBOutlet),我们会将当前的时候通过这个输出口显示出来。整个程序用到的主要有时间控制函数与计时器。

打开Classes文件夹中的LEDClockAppDelegate.h文件,这是一个应用程序委托的头文件,我们在其中添加一个NSTimer类的引用对象声明,同时添加一个无返回值的函数onInterval来实现时钟应用的计时功能,每隔一秒钟进行一次时钟计时.

//
//  LEDClockAppDelegate.h
//  LEDClock
//
//  Created by blessdyb on 09-9-5.
//  Copyright mobroad.com 2009. All rights reserved.
//

#import <UIKit/UIKit.h>

@class LEDClockViewController;

@interface LEDClockAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    LEDClockViewController *viewController;
	NSTimer *timer;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet LEDClockViewController *viewController;

-(void) onInterval;

@end

 

 

 

之后进入委托程序的实现文件LEDClockAppDelegate.h中(如果你是在LEDClockAppDelegate.h中,那点击option+command+↑,就可以直接跳转到相应的实现文件中)。

//
//  LEDClockAppDelegate.m
//  LEDClock
//
//  Created by blessdyb on 09-9-5.
//  Copyright mobroad.com 2009. All rights reserved.
//

#import "LEDClockAppDelegate.h"
#import "LEDClockViewController.h"

@implementation LEDClockAppDelegate

@synthesize window;
@synthesize viewController;


- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    
    timer=[NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(onInterval) userInfo:nil repeats:YES];  
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}

-(void) onInterval{
	[viewController interval];
}

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


@end

 

 

 

下面我们进行控制器类的编程实现。首先来看它的头文件,双击LEDClockViewController.h文件,我们在这里完成输入口的定义。由于我们的LED电子时钟是在一个标签上显示的,所以我们在这里声明一个UILabel的实例做为控制器类的属性,同时声明一个interval的无返回值方法(这下知道刚才委托类中的调用是怎么回事了吧)。

//
//  LEDClockViewController.h
//  LEDClock
//
//  Created by blessdyb on 09-9-5.
//  Copyright mobroad.com 2009. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface LEDClockViewController : UIViewController {
	IBOutlet UILabel *timerLabel;
}

@property (nonatomic, retain) UILabel *timerLabel;

-(void) interval;

@end

 

 

 

下面进入控制器类的实现文件中对刚才的定义进行实现,双击LEDClockViewController.m,我们首先需要设置整个程序视图加载时时钟显示标签的字体,大小及初始化文本。这样,在计时器开始运行后,我们只需要每过一秒种改变显示标签的文本值就可以了。

 

//
//  LEDClockViewController.m
//  LEDClock
//
//  Created by blessdyb on 09-9-5.
//  Copyright mobroad.com 2009. All rights reserved.
//

#import "LEDClockViewController.h"

@implementation LEDClockViewController

@synthesize timerLabel;

-(void) interval{
	NSUInteger unitFlags=NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
	NSCalendar *calendar=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
	NSDate *date=[NSDate date];
	NSDateComponents *now=[calendar components:unitFlags fromDate:date];
	int hour=[now hour];
	int minute=[now minute];
	int second=[now	second];
	[timerLabel setText:[NSString stringWithFormat:@"%02d:%02d:%02d",hour,minute,second]];
	[calendar release];
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[timerLabel setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:50.0]];
	[timerLabel setText:@"电子时钟"];
    [super viewDidLoad];
}




// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    //return (interfaceOrientation == UIInterfaceOrientationPortrait);
	return YES;
}


- (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 {
	[timerLabel release];
    [super dealloc];
}

@end

 

 

接着我们需要完成程序的界面设计及元素连接,我们来看Resources目录下的文件,这里有三个文件,一个是LEDClockViewController.xib,一个是MainWindow.nib(主要是让应用程序委托、主窗口和视图控制器实例在运行时创建),还有一个LEDClock-info.plist(应用程序的各种参数配置)文件。

         双击LEDClockViewController.xib文件,之后会默认打开Interface Builder,我们会看到一个文件管理器窗口,其中有File’s Owner,First Responder View.双击View图标后会出现一个窗口,这就是我们应用程序在运行时最终被加载的视图,我们在菜单栏选取ToolsInspector,之后就会出现一个View Attributes的窗口,在里面可以编辑视图的各种属性,在这里我们让它的背景(Background)为黑色。之后再选取ToolsLibrary,打开库面板,我们在Library中找到Label控件,用鼠标选中后拖放到刚才打开的View窗口中,同样打开属性选择器后更改当前Label控件的大小及字体色彩,在这里我们设置它的色彩为红色。

         最后是整个程序最重要的一个步骤,元素的连接,我们选中xib窗口中的File’s Owner图标,同时按住Ctrl按键后往视图上的Label控件方向拖动,此时会出现一根蓝色的线条,当这根线条到达Label控件后Label控件变为选中状态,之后就会出现一个Outlets框,选中timerLabel后单击就可以完成了。(这个步骤完成了后可以先选中Label控件后点ToolsConnections Inspector后会看到弹出的窗口中的Referencing Outlets中出现一个连接项)。

    这样整个程序就编写完成。我们选择Xcode中的Run后打开iPhone模拟器就可以看到整个程序的运行结果了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值