您的位置:首页 > 移动开发

iOS: 第一个iphone app

2012-03-05 12:33 495 查看
参考视频:http://s3.amazonaws.com/screencasts.pragmaticstudio.com/013_ib_xcode4.mov

1. run Xcode, select "create a new project > view-based application", input "project name" as "HelloWorld", "company identifier" as "edu.cityu.mobile" (company identifier相当于java的package),
uncheck "include unit tests"

2. select project path, uncheck "create local git repository for this project", and then click "Create"

3.看看project的目录结构:
* "HelloWorld"目录下包含的是files for your project (主要包含Objective-C class (.h file and .m file) and .xib files (xib file是用来包含interface的信息,相当于android的layout
xml file))。本例包含的文件有

>MainWindow.xib:应用程序主窗口。(窗口的东东稍后详解)

> HelloWorldAppDelegate.h和HelloWorldAppDelegate.m:应用程序的委派

> HelloWorldViewController.h和HelloWorldViewController.m:视图控制器,主要是界面的控件响应函数

> HelloWorldViewController.xib:应用程序默认的view
* "HelloWorld"目录下还有一个"Supporting files"目录,里面包含"main function file", an application wide header file "xxx-", 以及要用到的picture, data等。

>HelloWorld-Info.plist:应用程序配置文件,包含icon、名字、主窗口的nib文件(nib
== xib)等。相当于android的android-manifest.xml

> InfoPlist.string: i18n locale string info file

>main.m:main函数,应用程序入口

> HelloWorld_Prefix.pch:一些公用的头文件。在该文件里import的lib,将会缺省被import到所有的文件里
* "Frameworks"目录相当于eclipse的"lib reference",里面包含你可以引用的frameworks,例如Foundation framework, UIKit.framework等

* "products" 目录,project生成的产品就放在该目录

4. 添加a button and a label控件到你的.xib界面
1)click "HelloWorldViewController.xib" in the left "project navigator" panel,左边就会出现主界面的interface。
2)click xcode右上角toolbar的"View"区域

的第三个button "Hide or show the utilities",将会在xcode的最右边出现utilities
panel (它包含属性控制view,控件库的list等),该panel的下方区域,在toolbar

中点第三个"Show the object lib",就会出现可用的控件,然后drag and drop a button and a label to interface.

5. 把.xib绑定到ViewController上,因为只有绑定了,才能够让ViewController来控制.xib的控件以及处理.xib的event。 (.xib的绑定原理见 http://www.cnblogs.com/liubiqu/archive/2011/09/08/2171969.html)
1)还是click "HelloWorldViewController.xib" in the left "project navigator" panel, 然后在界面的左边有一个button named "File's owner"

,click
it
2)确认在右边的"utilities panel"的"custom class" tab的"class" field的值为HelloWorldViewController
3) click xcode右上角toolbar的"Editor"区域

的第二个button "show assistant editor"
,这样在界面窗口的右边就会出现对应的.h file editor窗口以便你进行界面和.h file的绑定
4) 为了方便操作, 可以先暂时隐藏左边的project navigator panel" and 右边的"utilities panel"
5)选择界面中的相应控件,创建outlet或
action方法。按住 Control键,从界面拖拽控件(如button,textbox,label等等)
— (注:或者右击并拖拽),到 @interface header文件中。

在本例中,把label控件拖拽绑定as IBOutlet,and 把button控件拖拽绑定as IBAction用于处理"tap inside" event handler

拖拽绑定的详解见:
* http://oleb.net/blog/2011/06/creating-outlets-and-actions-via-drag-and-drop-in-xcode-4/ * http://www.entlib.net/?p=897
做了绑定的动作,会自动生成下列代码
* 在对应的.h里添加了
@interface HelloWorldViewController :UIViewController {

UILabel *lblInfo;
}

@property (nonatomic,retain)IBOutletUILabel
*lblInfo;

- (IBAction)clickButton:(id)sender;

@end
* 在对应的.m里添加了
@synthesize lblInfo;
- (void)viewDidUnload
{
[selfsetLblInfo:nil];

。。。
}
- (void)dealloc {
[lblInforelease];

。。。
}
- (IBAction)clickButton:(id)sender {
}

创建绑定很方便,但是取消绑定就相对麻烦点。你需要以下几步:
step 1: show "utilities panel" first, 然后选择

"show the connection inspector"
step 2: 在界面上选择你已经绑定的控件,这时在"connection inspector"里就会列出该控件的绑定情况,你如果要取消绑定,就点对应connection的"x"
step 3: 上面的步骤只是取消了绑定,但之前创建绑定时自动生成的代码以及你自己添加的与该控件相关的代码依然还在,你要手动删除。(当然不删除也会编译运行成功)

取消后如何再度绑定:按住control键,把控件拖拽到已经存在于.h里的method/property上即可,详见参考视频

绑定实际上有多种方式,上面介绍的最常用的方式,其他方式详见http://s3.amazonaws.com/screencasts.pragmaticstudio.com/013_ib_xcode4.mov

6)添加button click handler的代码
- (IBAction)clickButton:(id)sender {
self.lblInfo.text=@"Hello
World!";
}

7)运行。搞定!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: