iPhone开发入门篇 “Hello World”分析代码

移动开发 iOS
本篇将介绍了Hello World程序的分析代码,也就是到底这个程序是怎么say Hello的.本文非常适合尚未入门的开发者,希望各位iPhone应用程序开发的初学者喜欢。

每个学习程序开发的第一个程序都是“Hello World”,作为刚刚入门的iPhone应用程序开发者,掌握“Hello World”的分析代码是十分重要的。本篇将介绍了Hello World程序的分析代码,也就是到底这个程序是怎么say Hello的。

51CTO推荐专题:iPhone应用程序开发初探

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

[[14914]]
 iPhone的Hello World程序

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

  1. IBOutlet UIWindow *window;  
  2. MyViewController *myViewController; 

其中第一行定义了程序的窗口,第二行定义了我们自己的界面.在 HelloWorldAppDelegate.m 文件中,函数

- (void)applicationDidFinishLaunching:(UIApplication *)application 是iPhone开发者经常要打交道的一个,定义了程序启动后要做的工作.这几行程序的任务是:指定 myViewController 为子界面,

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

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

  1.  
  2.  
  3. UIView *controllersView = [myViewController view];  
  4. [window addSubview:controllersView];  
  5. [window makeKeyAndVisible];  

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

iPhone界面 iPhone界面

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

定义程序 

在 MyViewController.m 文件中,

  1.  
  2.  
  3. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
  4. {  
  5. // Dismiss the keyboard when the view outside the text field is touched.  
  6. [textField resignFirstResponder];  
  7. // Revert the text field to the previous value.  
  8. textField.text = self.string;  
  9. [super touchesBegan:touches withEvent:event];  
  10. }  

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

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

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

  1.  
  2.  
  3. - (void)updateString {  
  4. // Store the text of the text field in the ‘string’ instance variable.  
  5. self.string = textField.text;  
  6. // Set the text of the label to the value of the ‘string’ instance variable.  
  7. label.text = self.string;  
  8. }  

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

好了,关于Hello World的分析代码就介绍到这,主要语句的功能都解说到了.希望大家喜欢。

本文选自淫雨霏霏的博客:http://lichen1985.com/blog/

【编辑推荐】

  1. 深入iPhone开发:应用程序核心探秘
  2. iPhone内存管理面面观 对象所有权与引用计数
  3. 专访最牛iPhone开发团队:走进移动开发
  4. iPad软件设计初步:它不只是大号的iPhone

 

责任编辑:佚名 来源: 淫雨霏霏博客
相关推荐

2016-09-06 17:43:12

SwiftCloudKit开发

2009-07-30 13:21:17

Scala入门Hello World

2015-07-30 09:43:10

独立游戏开发入门

2017-09-12 10:26:47

springbootmaven结构

2011-01-18 17:00:31

Postfix入门

2018-12-21 12:25:08

2009-06-15 17:22:36

JBoss Seam

2009-08-14 16:54:19

C# Hello Wo

2014-06-06 09:46:52

SwiftSwift教程

2020-11-16 10:19:33

Java

2009-06-09 13:02:30

NetBeans使用教程

2011-12-05 15:44:45

Knockout

2022-03-28 09:31:58

for循环语句

2012-01-17 10:47:07

jQuery

2020-11-13 07:22:46

Java基础While

2022-01-27 09:35:45

whiledo-while循环Java基础

2014-12-19 10:07:10

C

2017-11-23 17:45:46

Yii框架IntelYii框架深度剖析

2022-07-06 07:57:37

Zookeeper分布式服务框架

2021-12-21 09:02:31

Matplotlib Python可视化
点赞
收藏

51CTO技术栈公众号