实例iPhone SDK 编程入门教程.doc
实例 iPhone SDK 编程入门教程 - 第一天DAY ONE - MinutesToMidnight大家一起来建立我们的第一个 iPhone app,给你的iPhone计算离午夜12:00点的剩余时间。首先运行以安装好的 xCode选择: File->New Project.从 "New Project" 窗口 选择 : iPhone OS ->Applications-> View-Based Application命名 : 我这里命名为 "MinutesToMidnight"(1) UIView 界面设置- 黑色背景,红色字体,让它看起来像一个闹钟。双击文件: "MinutesToMidnightViewController.xib" ;然后 "Interface Builder" 会自动打开,在这里我们可以编辑改变界面 选择: Tools -> Reveal In Document Window -> View选择: Tools -> Attributes Inspector在色条内选择 "黑色",可以看到背景变为黑色(2) 加入 UILabel 显示我们的倒数时间选择: Tools -> Library ; 从Library显示菜单中拖拉一个 Label 到 Main View在主显示上点击 Label;从Label Attributes 上选着字体的颜色和字体大小。(3) 把界面文件更新到我们的代码文件上面在MinutesToMidnightViewController.xib文件窗口, 选择: File's Owner (这步很关键,我在第一次编辑时没成功就因为忘了这一步)选择: Tools->Idenity Inspector在 Idenity inspector 中间 "Class Outlets" 按 "+";把 myOutlet1 改为 "countdownLabel";把 id 改为 "UILabel"; 按回车,确定已更改。(4) 写入"MinutesToMidnightViewController.h" 和 "MinutesToMidnightViewController.m"确认还在MinutesToMidnightViewController.xib文件窗口, 选择: File's Owner选择: File->Write Class Files在 Save As: "MinutesToMidnightViewController"点击: Save点击: Merge在右下角 "Actions" 按下选 (bottom right) "Select Left"点击: 上方红色小圈关闭窗口点击: Save关闭所有 merge 窗口打开 xCode; 选择 Build->Build(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 以上几步,已成功完成界面了,下面我们继续把程式添加我们的代码文件。(6) 在xCode打开 MinutesToMidnightAppDelegate.h 文件没修改前#import <UIKit/UIKit.h>class MinutesToMidnightViewController;interface MinutesToMidnightAppDelegate : NSObject <UIApplicationDelegate> UIWindow *window; MinutesToMidnightViewController *viewController;property (nonatomic, retain) IBOutlet UIWindow *window;property (nonatomic, retain) IBOutlet MinutesToMidnightViewController *viewController;end没修改前修改后#import <UIKit/UIKit.h>class MinutesToMidnightViewController;interface MinutesToMidnightAppDelegate : NSObject <UIApplicationDelegate> IBOutlet UIWindow *window; IBOutlet MinutesToMidnightController *viewController; NSTimer *timer;-(void)onTimer;property (nonatomic, retain) IBOutlet UIWindow *window;property (nonatomic, retain) IBOutlet MinutesToMidnightViewController *viewController;end(7) 在xCode打开 MinutesToMidnightAppDelegate.m 文件没修改前#import "MinutesToMidnightAppDelegate.h"#import "MinutesToMidnightViewController.h"implementation MinutesToMidnightAppDelegatesynthesize window;synthesize viewController;- (void)applicationDidFinishLaunching:(UIApplication *)application / Override point for customization after app launch window addSubview:viewController.view; window makeKeyAndVisible;- (void)dealloc viewController release; window release; super dealloc;end没修改前修改后#import "MinutesToMidnightAppDelegate.h"#import "MinutesToMidnightViewController.h"implementation MinutesToMidnightAppDelegatesynthesize window;synthesize viewController;- (void)applicationDidFinishLaunching:(UIApplication *)application timer = NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:selector(onTimer) userInfo:nil repeats:YES; 不换行 / Override point for customization after app launch window addSubview:viewController.view; window makeKeyAndVisible;- (void)onTimer viewController updateLabel;- (void)applicationWillTerminate:(UIApplication *)application timer invalidate;- (void)dealloc timer release; viewController release; window release; super dealloc;end修改后(8) 在xCode打开 MinutesToMidnightViewController.m 文件修改前#import "MinutesToMidnightViewController.h"implementation MinutesToMidnightViewController- (void)didReceiveMemoryWarning super didReceiveMemoryWarning;- (void)dealloc super dealloc;end修改前修改后#import "MinutesToMidnightViewController.h"implementation MinutesToMidnightViewController- (void)viewDidLoad countdownLabel setFont:UIFont fontWithName:"DBLCDTempBlack" size:128.0; countdownLabel.text = "I0A0IN6" super viewDidLoad;- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation return (interfaceOrientation = UIInterfaceOrientationPortrait);- (void)didReceiveMemoryWarning super didReceiveMemoryWarning; / Releases the view if it doesn't have a superview / Release anything that's not essential, such as cached data- (void)dealloc super dealloc;-(void)updateLabel NSDate* now = NSDate date; int hour = 23 - now dateWithCalendarFormat:nil timeZone:nil hourOfDay; int min = 59 - now dateWithCalendarFormat:nil timeZone:nil minuteOfHour; int sec = 59 - now dateWithCalendarFormat:nil timeZone:nil secondOfMinute; countdownLabel.text = NSString stringWithFormat:"%02d:%02d:%02d", hour, min,sec; end修改后最后在 xCode 选择 Build->Build and Go下载今天程序文件: MinsToMidnight.zip 实例 iPhone SDK 编程入门教程,来源于下面文章本人根据appsamuck这个网站加以翻译,修改及测试,希望大家多多交流。一切版权归 所有。实例 iPhone SDK 编程入门教程 - 第二天Day Two Bonfire今天试试运用 UIImageView 来制作一个"营火动画"首先运行以安装好的 xCode,和第一天一样选择: File->New Project.从 "New Project" 窗口 选择: iPhone OS ->Applications-> View-Based Application命名: 我这里命名为"Bonfire"(1)在xcode打开BonfireViewController.m 文件在-(void)viewDidLoad 里面添加代码,如下-(void)viewDidLoad/ create the view that will execute our animationUIImageView* campFireView = UIImageView alloc initWithFrame:self.view.frame; / load all the frames of our animation campFireView.animationImages = NSArray arrayWithObjects: UIImage imageNamed:"campFire01.gif", UIImage imageNamed:"campFire02.gif", UIImage imageNamed:"campFire03.gif", UIImage imageNamed:"campFire04.gif", UIImage imageNamed:"campFire05.gif", UIImage imageNamed:"campFire06.gif", UIImage imageNamed:"campFire07.gif", UIImage imageNamed:"campFire08.gif", UIImage imageNamed:"campFire09.gif", UIImage imageNamed:"campFire10.gif", UIImage imageNamed:"campFire11.gif", UIImage imageNamed:"campFire12.gif", UIImage imageNamed:"campFire13.gif", UIImage imageNamed:"campFire14.gif", UIImage imageNamed:"campFire15.gif", UIImage imageNamed:"campFire16.gif", UIImage imageNamed:"campFire17.gif", nil; / all frames will execute in 1.75 seconds campFireView.animationDuration = 1.75; / repeat the annimation forever campFireView.animationRepeatCount = 0; / start animating campFireView startAnimating; / add the animation view to the main window self.view addSubview:campFireView;(2)下载图片文件 images.zip 把下载好的文件解压储存在与程序同一文件夹里在xcode - Group & Files - 在文件夹 Resource 上按鼠标右键Add->Existing files 选择下载好的Defult.png 图片; 同上在文件夹Resource 上按鼠标右键 Add-> New Group, 命名为images; 在 文件夹images 上按鼠标右键 Add->Existing files 选择下载好的在images里的所有图片。最后在xCode 选择 Build->Build and Go, save all 保存所有文件下载今天程序文件: Bonfire.zip实例 iPhone SDK 编程入门教程 - 第三天DAY THREE - OPEN URL大家一起来建立一个 iPhone app,给你的iPhone打开google地图有效地址连接。首先运行以安装好的 xCode选择: File->New Project.从 "New Project" 窗口 选择 : iPhone OS ->Applications-> View-Based Application命名 : 我这里命名为 "OpenURL"(1) 在xCode打开 OpenURLViewController.h 文件,加入下面红色代码#import <UIKit/UIKit.h>imterface OpenURLViewController : UIViewController-(IBaction)openMaps;end (2) 在xCode打开 OpenURLViewController.m 文件,加入下面红色代码#import <UIKit/UIKit.h#import "Day3ViewController.h"implementation Day3ViewController/ 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;-(IBAction)openMaps NSString* addressText = "1 Queen st, Auckland, NZ" addressText = addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding; NSString* urlText = NSString stringWithFormat:" /NSlog(urlText); UIApplication sharedApplication openURL:NSURL URLWithString:urlText;/ Override to allow orientations other than the default portrait orientation.- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation / Return YES for supported orientations return (interfaceOrientation = UIInterfaceOrientationPortrait);- (void)didReceiveMemoryWarning super didReceiveMemoryWarning; / Releases the view if it doesn't have a superview / Release anything that's not essential, such as cached data- (void)dealloc super dealloc;end(3) UIView 界面设置- 黑色背景,红色字体,让它看起来像一个闹钟。双击文件: "OpenURLViewController.xib" ;然后 "Interface Builder" 会自动打开,在这里我们可以编辑改变界面 选择: Tools -> Reveal In Document Window -> View选择: Tools -> Attributes Inspector在色条内选择 "黑色",可以看到背景变为黑色(4) 加入 Button ,点击后打开连接选择: Tools -> Library ;从Library显示菜单中拖拉一个 Button 到 Main View在主显示上点击 Button;从Button Attributes 里面Title内填上 "Open Google Map"。(5)从 Interface Builder 写入 UIButton 的 class file再打开SDK工具 Interface Builder在主视窗口或文件窗口;点击 Button选择: Tools -> Connection Inspector移动鼠标在"Touch Up Inside" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"File's Owner"放开鼠标选择键出现 "openMaps" 选上它。选择: File -> Save then close Interface Builde 最后在 xCode 选择 Build->Build and Go 下载今天程序文件: OpenURL.zip 实例 iPhone SDK 编程入门教程 - 第四天DAY Four What is my IP?大家一起来建立一个 iPhone app,给你的iPhone显示 现时 的ip地址。首先运行以安装好的 xCode选择: File->New Project.从 "New Project" 窗口 选择 : iPhone OS ->Applications-> View-Based Application命名 : 我这里命名为 "MYIP"(1) 在xCode打开 MYIPViewController.h 文件,加入下面红色代码#import <UIKit/UIKit.h>imterface OpenURLViewController : UIViewController IBOutlet UILabel *showIPlabel;end (2) 在xCode打开 MYIPViewController.m 文件,加入下面红色代码#import <UIKit/UIKit.h#import "MYIPViewController.h"implementation MYIPViewController/ 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) showIPlabel.text = "" return self;-(NSString *)getAddress char iphone_ip255; strcpy(iphone_ip,"127.0.0.1"); / if everything fails NSHost* myhost =NSHost currentHost; if (myhost) NSString *ad = myhost address; if (ad) strcpy(iphone_ip,ad cStringUsingEncoding:NSASCIIStringEncoding); return NSString stringWithFormat:"%s",iphone_ip;- (void)viewDidLoad NSString* address = self getAddress; NSString* myIPAdress = NSString stringWithFormat:"IP Address: %", address; showIPlabel.text = myIPAdress; super viewDidLoad;/ Override to allow orientations other than the default portrait orientation.- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation / Return YES for supported orientations return (interfaceOrientation = UIInterfaceOrientationPortrait);- (void)dealloc super dealloc;end (3) UIView 界面设置双击文件: "MYIPViewController.xib" ;然后 "Interface Builder" 会自动打开,在这里我们可以编辑改变界面 (4) 加入 Label ,显示iPhone 的IP地址选择: Tools -> Library ; 从Library显示菜单中拖拉一个 Label 到 Main View(5)从 Interface Builder 写入 UILabel 的 class fileSDK工具 Interface Builder在主视窗口或文件窗口;点击 Label选择: Tools -> Connection Inspector移动鼠标在"Touch Up Inside" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"File's Owner"放开鼠标选择键出现 "showIPlabel" 选上它。选择: File -> Save then close Interface Builde 以上几步,已成功完成界面了,下面我们继续把程式添加我们的代码文件。最后在 xCode 选择Build->Build and Go 下载今天程序文件: WhatIsMyIp.zip 实例 iPhone SDK 编程入门教程 - 第五天DAY Five Count Me In今天给你的iPhone 制作一个简单的数数程序。 纲要:在程序显示前运行代码;UIButton, UILabel, UIImageView 的运用;把integer转换为string;首先运行以安装好的 xCode选择: File->New Project.从 "New Project" 窗口 选择 : iPhone OS ->Applications-> View-Based Application命名 : 我这里命名为 “CountMeIn” (1) 在xCode打开 CountMeInViewController.h 文件,加入下面代码#import <UIKit/UIKit.h> interface CountMeInViewController : UIViewController IBOutlet UILabel *counter;-(IBAction)reset;-(IBAction)addUnit;-(IBAction)subtractUnit; end (2) 在xCode打开 CountMeInViewController.m 文件,加入下面代码#import "CountMeInViewController.h" implementation CountMeInViewController int count = 0; -(IBAction)reset count = 0; counter.text = "0" - (IBAction)addUnit if(count >= 999) return; NSString *numValue = NSString alloc initWithFormat:"%d", count+; counter.text = numValue; numValue release; - (IBAction)subtractUnit if(count <= 0) return; NSString *numValue = NSString alloc initWithFormat:"%d", count-; counter.text = numValue; numValue 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 viewDidLoad to do additional setup after loading the view, typically from a nib.- (void)viewDidLoad counter.text = "0" super viewDidLoad;/ Override to allow orientations other than the defa