iOS中Xcode和Interface Builder联合应用 实例操作

移动开发 iOS
要想让Xcode和Interface Builder同时开发一个应用程序,必须以模版View-Based Application(基于视图的应用程序)作为应用程序的起点。先来看内容

iOSXcode和Interface Builder联合应用 实例操作是本文要介绍的内容,刚刚接触ios有很多地方都还不知道,但对它有这强烈的兴趣。现在先总结一下这几天掌握的东西。要想让Xcode和Interface Builder同时开发一个应用程序,必须以模版View-Based Application(基于视图的应用程序)作为应用程序的起点。

1、创建项目

因为使用的测试程序工具是iPhone Simulator,所以在创建项目时选的是iPhone OS的Application,并在右边的列表中选择View-Based Application,最好还是勾选Use Core Data for storage这个项。

2、创建输出和操作

根据应用程序的设计,假设这里要使用三个文本框,两个文本视图,一个按钮。则先在视图控制器的开头(在项目中的Class文件下的一个文件)*ViewController.h创建输出口和操作。在在类内声明变量: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通常有相应的编译指令@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; 

到这里,设置已经基本完成了,现在要做的就是对界面的设计了~

3、打开视图控制器

在项目文档下的一个名为Resources的文件夹下有一个名为:*ViewController.xib文件,这是一个视图控制器文件,利用IB(Interface Builder)对界面的设计都是在这个文件下进行的。打开刚文件,然后双击View图标,对这个空视图进行编辑。

4、添加各视图元件

根据刚才的设计,向空视图中添加元件,打开Tools-->Library,将呈现很多视图元件,根据需要进行添加。

5、设置各视图元件的属性

选择要设置的元件,然后打开Tools-->Attributes Inspector,进行属性的设置。

6、将所需元件连接到输出口

根据项目要求,将视图中的个元件连接到Xcode定义的输出口(Outlets)对应的变量中。按住Control键并从文档窗口中的File's Owner图标拖拽到对应的元件图标上,并选择相对应的变量。

7、连接到操作

在这个项目中,只有一个操作,那就是有触摸按钮产生的一个操作事件。因为之前有在有文件中有声明一个createStory方法,选择该按钮元件,并打开Tools-->Connections Inspector,将事件Touch Up Inside旁边的圆圈拖拽到Interface Builder文档中的File's Owner图标中,提示选择方法时选择createStory。在设置个元件属性的时候,也可能用到的一个事件是Did End On Exit,连接操作的方法同本例。

8、实现视图控制器逻辑

为了完成该项目,还需编写实现代码,实现代码将编写在*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、释放对象

在应用程序中使用完对象后,总是应释放它以释放内存,这是一种良好的编程习惯,也是处于一种安全性的考虑,可以尽量减少内存泄漏!下列是本例中,释放对象的代码:

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

小结:iOSXcode和Interface Builder联合应用 实例操作的内容介绍完了,希望本文对你有所帮助!!!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-08-05 10:01:23

Xcode Interface

2011-07-28 13:47:20

Xcode Interface

2011-07-19 15:55:09

Xcode Interface Builder

2011-08-03 14:13:45

Xcode 4 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-08-05 09:48:46

iPhone Interface

2011-07-22 15:56:18

iPhone Interface Builder

2011-07-19 13:20:22

Xcode

2011-07-29 13:40:00

Xcode iOS 4.2 iPhone

2011-07-26 16:28:11

Xcode iPad

2011-07-19 18:17:38

Xcode Xcode4 App

2011-08-05 09:38:46

Interface B Cocoa 界面

2011-08-11 15:12:06

Xcode检验

2015-03-30 09:32:15

XcodeiOS应用程序

2011-07-06 10:22:31

XCode IOS object-C

2013-09-03 11:06:05

Windows

2013-09-02 16:59:34

JavaScriptHTML

2020-09-23 09:08:05

typescript
点赞
收藏

51CTO技术栈公众号