雨雪霏霏 – iPhone博客

29 Mar, 2009

iPhone开发学习手记–Hello World(分析篇)

Posted by: 雨雪霏霏 In: iPhone开发教程

雨雪霏霏的博客在之前一篇iPhone开发学习手记中介绍了Hello World程序的下载和模拟运行,本篇将分析代码,也就是到底这个程序是怎么say Hello的.本文适合尚未入门的开发者,有经验的开发员可以飘过本篇,或者留下来提点宝贵意见:)

这个程序基本的运行顺序是:载入窗口(UIWindow)->载入自定义的界面(MyViewController),而各种消息的处理均在自定义的界面当中.而程序的设计遵循了MVC(Model-View-Controller)方法,也就是界面和程序是分开做的,通过controller联接彼此.



首先看窗口.在 HelloWorldAppDelegate.h 文件当中有这样两行:

IBOutlet UIWindow *window;
MyViewController *myViewController;

其中第一行定义了程序的窗口,第二行定义了我们自己的界面.在 HelloWorldAppDelegate.m 文件中,函数
- (void)applicationDidFinishLaunching:(UIApplication *)application 是iPhone开发者经常要打交道的一个,定义了程序启动后要做的工作.这几行程序的任务是:指定 myViewController 为子界面,

MyViewController *aViewController = [[MyViewController alloc] initWithNibName:@”HelloWorld” bundle:[NSBundle mainBundle]];
self.myViewController = aViewController;
[aViewController release];

并把子界面显示到上面来.

UIView *controllersView = [myViewController view];
[window addSubview:controllersView];
[window makeKeyAndVisible];

前面提到了,程序设计遵循了MVC方法,但我们还没介绍代码和界面之间是怎么联系的,也就是说,我们说了程序的UIWindow和view controller要干什么什么,也画出了界面,可iPhone怎么知道哪个类对应哪个界面呢?这个是在IB(Interface Builder)中完成的.请双击 HelloWorld.xib 打开IB.下面看的就是我们的界面.

点到File’s Owner,在Identity Viewer中,注意Class为MyViewController,这就定义了Model和View之间的对应关系.在同一个xib文件中,File’s Owner设定为一个类,并指向其View,该对应关系就建立好了.

在 MyViewController.m 文件中,

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Dismiss the keyboard when the view outside the text field is touched.
[textField resignFirstResponder];
// Revert the text field to the previous value.
textField.text = self.string;
[super touchesBegan:touches withEvent:event];
}

的作用是:对触摸做出响应.当触摸在键盘外时,通过 resignFirstResponder 撤销键盘.

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
// When the user presses return, take focus away from the text field so that the keyboard is dismissed.
if (theTextField == textField) {
[textField resignFirstResponder];
// Invoke the method that changes the greeting.
[self updateString];
}
return YES;
}

的作用是:当输入完文字并按Return后,隐藏键盘,并调用updateString命令来更新显示.这个命令如下:

- (void)updateString {

// Store the text of the text field in the ‘string’ instance variable.
self.string = textField.text;
// Set the text of the label to the value of the ‘string’ instance variable.
label.text = self.string;
}

简单的说就是用输入的文字来替换标签原来的文字以更新显示.

好了,关于Hello World就介绍到这,主要语句的功能都解说到了.写技术类的博客有时就是很累,因为很多细节是需要程序员自己去折腾的,而作者又希望能介绍的更多,减少程序员们折腾的时间,就成了一个矛盾.雨雪霏霏的iPhone博客会有更多的技巧性文章,比如介绍怎样调用系统的图片库,欢迎大家关注并提出意见建议!

7 Responses to "iPhone开发学习手记–Hello World(分析篇)"

1 | iPhone学习开发手记 - 隐藏Status Bar和修改程序名称的小技巧 | 雨雪霏霏 - iPhone博客

April 11th, 2009 at 1:46 am

Avatar

[...] ————————————————————————- 雨雪霏霏的iPhone学习开发笔记,主要是将自己解决一些初级确很重要的问题的经验写出来,帮助大家更快找到答案.有兴趣读者的请看之前两篇:Hello World(分析篇)和Hello World(搭建篇). Tags: iPhone, Status Bar, 学习, 开发, 状态条, 编程, 雨雪霏霏 [...]

2 | Forrest

March 10th, 2010 at 7:57 pm

Avatar


写技术类的博客有时就是很累,因为很多细节是需要程序员自己去折腾的,而作者又希望能介绍的更多,减少程序员们折腾的时间,就成了一个矛盾.

难免要问,为什么啊,只是兴趣爱好,做公益性的老师?

3 | 雨雪霏霏

March 10th, 2010 at 9:02 pm

Avatar

对我来说,一方面是兴趣爱好,另一方面,写这些东西时我自己也温习了一遍,对自己也有好处。

不过你说得对,写这些很累,因此我的更新频率比较低。。。

4 | 杨亚民

April 12th, 2010 at 1:30 am

Avatar

这样一个情景:
在iphone 应用程序中,在一个视图下嵌套一个 网页,行吗?

That it is :
我在本地的客户端程序中要在一个界面 中显示一个网页的内容,还要保证这个网页运行 的效果,和在浏览器中的效果一样?

5 | 雨雪霏霏

April 12th, 2010 at 11:05 am

Avatar

没有问题,调用UIWebView就可以

6 | cgao

August 16th, 2010 at 3:53 pm

Avatar

请问如果想做个高校bbs的app 应该有哪些条件呢 有没有专门介绍这个的文章? 谢谢~

7 | 雨雪霏霏

August 20th, 2010 at 1:30 pm

Avatar

给高校做非bbs的app我做过,只要有一些网络接口就行。bbs你可以去cocoa china问一下。

Comment Form

About

About Google, about Apple, about phones and browsers, about science and technologies.

Subscribe