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

iOS中Xcode和Interface Builder聯(lián)合應(yīng)用 實(shí)例操作

移動開發(fā) iOS
要想讓Xcode和Interface Builder同時開發(fā)一個應(yīng)用程序,必須以模版View-Based Application(基于視圖的應(yīng)用程序)作為應(yīng)用程序的起點(diǎn)。先來看內(nèi)容

iOSXcode和Interface Builder聯(lián)合應(yīng)用 實(shí)例操作是本文要介紹的內(nèi)容,剛剛接觸ios有很多地方都還不知道,但對它有這強(qiáng)烈的興趣。現(xiàn)在先總結(jié)一下這幾天掌握的東西。要想讓Xcode和Interface Builder同時開發(fā)一個應(yīng)用程序,必須以模版View-Based Application(基于視圖的應(yīng)用程序)作為應(yīng)用程序的起點(diǎn)。

1、創(chuàng)建項(xiàng)目

因?yàn)槭褂玫臏y試程序工具是iPhone Simulator,所以在創(chuàng)建項(xiàng)目時選的是iPhone OS的Application,并在右邊的列表中選擇View-Based Application,最好還是勾選Use Core Data for storage這個項(xiàng)。

2、創(chuàng)建輸出和操作

根據(jù)應(yīng)用程序的設(shè)計(jì),假設(shè)這里要使用三個文本框,兩個文本視圖,一個按鈕。則先在視圖控制器的開頭(在項(xiàng)目中的Class文件下的一個文件)*ViewController.h創(chuàng)建輸出口和操作。在在類內(nèi)聲明變量:eg:

  1. @interface *ViewComtroller : UIViewController {  
  2. IBOutlet UITextField *thePlace;  
  3. IBOutlet UITextField *theVerb;  
  4. IBOutlet UITextField *theNumber;  
  5. IBOutlet UITextView *theStory;  
  6. IBOutlet UITextField *theTemplate;  
  7. IBOutlet UIButton *generateStory;  
  8. }  
  9. @propery (retain, nonatomic) UITextField *thePlace;  
  10. @propery (retain, nonatomic) UITextField *theVerb;  
  11. @propery (retain, nonatomic) UITextField *Number;  
  12. @propery (retain, nonatomic) UITextView *theStory;  
  13. @propery (retain, nonatomic) UITextView *theTemplator;  
  14. @propery (retain, nonatomic) UIButton *generateStory;  
  15. -(IBAction) createStory: (id)  sender;  
  16. @end 

@propery通常有相應(yīng)的編譯指令@synthesize,所以需要在*.ViewController.m中的編譯指令@implementation后面添加下列紅色字體代碼:

  1. @implementation *ViewXontroller  
  2. @synthesize thePlace;  
  3. @synthesize theVerb;  
  4. @synthesize theNumber;  
  5. @synthesize theStory;  
  6. @synthesize theTemplate;  
  7. @synthesize generateStory; 

到這里,設(shè)置已經(jīng)基本完成了,現(xiàn)在要做的就是對界面的設(shè)計(jì)了~

3、打開視圖控制器

在項(xiàng)目文檔下的一個名為Resources的文件夾下有一個名為:*ViewController.xib文件,這是一個視圖控制器文件,利用IB(Interface Builder)對界面的設(shè)計(jì)都是在這個文件下進(jìn)行的。打開剛文件,然后雙擊View圖標(biāo),對這個空視圖進(jìn)行編輯。

4、添加各視圖元件

根據(jù)剛才的設(shè)計(jì),向空視圖中添加元件,打開Tools-->Library,將呈現(xiàn)很多視圖元件,根據(jù)需要進(jìn)行添加。

5、設(shè)置各視圖元件的屬性

選擇要設(shè)置的元件,然后打開Tools-->Attributes Inspector,進(jìn)行屬性的設(shè)置。

6、將所需元件連接到輸出口

根據(jù)項(xiàng)目要求,將視圖中的個元件連接到Xcode定義的輸出口(Outlets)對應(yīng)的變量中。按住Control鍵并從文檔窗口中的File's Owner圖標(biāo)拖拽到對應(yīng)的元件圖標(biāo)上,并選擇相對應(yīng)的變量。

7、連接到操作

在這個項(xiàng)目中,只有一個操作,那就是有觸摸按鈕產(chǎn)生的一個操作事件。因?yàn)橹坝性谟形募杏新暶饕粋€createStory方法,選擇該按鈕元件,并打開Tools-->Connections Inspector,將事件Touch Up Inside旁邊的圓圈拖拽到Interface Builder文檔中的File's Owner圖標(biāo)中,提示選擇方法時選擇createStory。在設(shè)置個元件屬性的時候,也可能用到的一個事件是Did End On Exit,連接操作的方法同本例。

8、實(shí)現(xiàn)視圖控制器邏輯

為了完成該項(xiàng)目,還需編寫實(shí)現(xiàn)代碼,實(shí)現(xiàn)代碼將編寫在*ViewController.m中,如本例需添加下列代碼:

  1. -(IBAction) createStory: (id) sender {  
  2.  theStory.text  = [theTemplate.text  
  3.        stringByReplacingOccurrencesOfString:@"<place>"  
  4.        withString:thePlace.text];  
  5.  theStory.text = [theStory.text  
  6.       stringByReplacingOccurrencesOfString:@"<verb>"  
  7.       withString:theVerb.text];  
  8.  theStory.text = [theStory.text  
  9.       stringByReplacingOccurrencesOfString:@"<number>"  
  10.       withString:theNumber.text];  
  11.  
  12. } 

9、釋放對象

在應(yīng)用程序中使用完對象后,總是應(yīng)釋放它以釋放內(nèi)存,這是一種良好的編程習(xí)慣,也是處于一種安全性的考慮,可以盡量減少內(nèi)存泄漏!下列是本例中,釋放對象的代碼:

  1. - (void)dealloc{  
  2.  [thePlace release];  
  3.  [theVerb release];  
  4.  [theNumber release];  
  5.  [theStory  release];  
  6.  [theTemplate release];  
  7.  [generateStory release];  
  8.    [super dealloc];  

小結(jié):iOSXcode和Interface Builder聯(lián)合應(yīng)用 實(shí)例操作的內(nèi)容介紹完了,希望本文對你有所幫助?。?!

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-08-05 10:01:23

Xcode Interface

2011-07-19 15:55:09

Xcode Interface Builder

2011-08-03 14:13:45

Xcode 4 Interface

2011-07-28 13:47:20

Xcode Interface

2011-07-20 09:49:41

Xcode Interface Builder

2011-07-06 15:06:46

Xcode Cocoa

2011-07-19 17:05:22

Xcode Libary

2011-07-22 15:56:18

iPhone Interface Builder

2011-07-19 13:20:22

Xcode

2011-08-05 09:48:46

iPhone Interface

2011-07-26 16:28:11

Xcode iPad

2011-07-19 18:17:38

Xcode Xcode4 App

2011-07-29 13:40:00

Xcode iOS 4.2 iPhone

2011-08-05 09:38:46

Interface B Cocoa 界面

2011-08-11 15:12:06

Xcode檢驗(yàn)

2013-09-02 16:59:34

JavaScriptHTML

2013-09-03 11:06:05

Windows

2009-08-27 13:30:11

C# interfac

2020-09-23 09:08:05

typescript

2015-03-30 09:32:15

XcodeiOS應(yīng)用程序
點(diǎn)贊
收藏

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