自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone開發(fā)入門篇 “Hello World”分析代碼

移動開發(fā) iOS
本篇將介紹了Hello World程序的分析代碼,也就是到底這個程序是怎么say Hello的.本文非常適合尚未入門的開發(fā)者,希望各位iPhone應(yīng)用程序開發(fā)的初學(xué)者喜歡。

每個學(xué)習(xí)程序開發(fā)的第一個程序都是“Hello World”,作為剛剛?cè)腴T的iPhone應(yīng)用程序開發(fā)者,掌握“Hello World”的分析代碼是十分重要的。本篇將介紹了Hello World程序的分析代碼,也就是到底這個程序是怎么say Hello的。

51CTO推薦專題:iPhone應(yīng)用程序開發(fā)初探

這個程序基本的運行順序是:載入窗口(UIWindow)->載入自定義的界面(MyViewController),而各種消息的處理均在自定義的界面當(dāng)中.而程序的設(shè)計遵循了MVC(Model-View-Controller)方法,也就是界面和程序是分開做的,通過controller聯(lián)接彼此.

[[14914]]
 iPhone的Hello World程序

首先看窗口.在 HelloWorldAppDelegate.h 文件當(dāng)中有這樣兩行:

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

其中第一行定義了程序的窗口,第二行定義了我們自己的界面.在 HelloWorldAppDelegate.m 文件中,函數(shù)

- (void)applicationDidFinishLaunching:(UIApplication *)application 是iPhone開發(fā)者經(jīng)常要打交道的一個,定義了程序啟動后要做的工作.這幾行程序的任務(wù)是:指定 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];  

前面提到了,程序設(shè)計遵循了MVC方法,但我們還沒介紹代碼和界面之間是怎么聯(lián)系的,也就是說,我們說了程序的UIWindow和view controller要干什么什么,也畫出了界面,可iPhone怎么知道哪個類對應(yīng)哪個界面呢?這個是在IB(Interface Builder)中完成的.請雙擊 HelloWorld.xib 打開IB.下面看的就是我們的界面.

iPhone界面 iPhone界面

點到File’s Owner,在Identity Viewer中,注意Class為MyViewController,這就定義了Model和View之間的對應(yīng)關(guān)系.在同一個xib文件中,File’s Owner設(shè)定為一個類,并指向其View,該對應(yīng)關(guān)系就建立好了.

定義程序 

在 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. }  

的作用是:對觸摸做出響應(yīng).當(dāng)觸摸在鍵盤外時,通過 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. }  

作用是:當(dāng)輸入完文字并按Return后,隱藏鍵盤,并調(diào)用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. }  

簡單的說就是用輸入的文字來替換標(biāo)簽原來的文字以更新顯示.

好了,關(guān)于Hello World的分析代碼就介紹到這,主要語句的功能都解說到了.希望大家喜歡。

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

【編輯推薦】

  1. 深入iPhone開發(fā):應(yīng)用程序核心探秘
  2. iPhone內(nèi)存管理面面觀 對象所有權(quán)與引用計數(shù)
  3. 專訪最牛iPhone開發(fā)團(tuán)隊:走進(jìn)移動開發(fā)
  4. iPad軟件設(shè)計初步:它不只是大號的iPhone

 

責(zé)任編輯:佚名 來源: 淫雨霏霏博客
相關(guān)推薦

2016-09-06 17:43:12

SwiftCloudKit開發(fā)

2009-07-30 13:21:17

Scala入門Hello World

2015-07-30 09:43:10

獨立游戲開發(fā)入門

2011-01-18 17:00:31

Postfix入門

2017-09-12 10:26:47

springbootmaven結(jié)構(gòu)

2024-04-11 13:13:27

2018-12-21 12:25:08

2009-06-15 17:22:36

JBoss Seam

2009-08-14 16:54:19

C# Hello Wo

2020-11-16 10:19:33

Java

2009-06-09 13:02:30

NetBeans使用教程

2014-06-06 09:46:52

SwiftSwift教程

2022-01-27 09:35:45

whiledo-while循環(huán)Java基礎(chǔ)

2020-11-13 07:22:46

Java基礎(chǔ)While

2012-01-17 10:47:07

jQuery

2022-03-28 09:31:58

for循環(huán)語句

2014-12-19 10:07:10

C

2017-11-23 17:45:46

Yii框架IntelYii框架深度剖析

2011-12-05 15:44:45

Knockout

2022-07-06 07:57:37

Zookeeper分布式服務(wù)框架
點贊
收藏

51CTO技術(shù)棧公眾號