iOS中Xcode和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:
@interface *ViewComtroller : UIViewController {
IBOutlet UITextField *thePlace;
IBOutlet UITextField *theVerb;
IBOutlet UITextField *theNumber;
IBOutlet UITextView *theStory;
IBOutlet UITextField *theTemplate;
IBOutlet UIButton *generateStory;
}
@propery (retain, nonatomic) UITextField *thePlace;
@propery (retain, nonatomic) UITextField *theVerb;
@propery (retain, nonatomic) UITextField *Number;
@propery (retain, nonatomic) UITextView *theStory;
@propery (retain, nonatomic) UITextView *theTemplator;
@propery (retain, nonatomic) UIButton *generateStory;
-(IBAction) createStory: (id) sender;
@end
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
@propery通常有相应的编译指令@synthesize,所以需要在*.ViewController.m中的编译指令@implementation后面添加下列红色字体代码:
@implementation *ViewXontroller
@synthesize thePlace;
@synthesize theVerb;
@synthesize theNumber;
@synthesize theStory;
@synthesize theTemplate;
@synthesize generateStory;
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
到这里,设置已经基本完成了,现在要做的就是对界面的设计了~
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中,如本例需添加下列代码:
-(IBAction) createStory: (id) sender {
theStory.text = [theTemplate.text
stringByReplacingOccurrencesOfString:@"<place>"
withString:thePlace.text];
theStory.text = [theStory.text
stringByReplacingOccurrencesOfString:@"<verb>"
withString:theVerb.text];
theStory.text = [theStory.text
stringByReplacingOccurrencesOfString:@"<number>"
withString:theNumber.text];
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
9、释放对象
在应用程序中使用完对象后,总是应释放它以释放内存,这是一种良好的编程习惯,也是处于一种安全性的考虑,可以尽量减少内存泄漏!下列是本例中,释放对象的代码:
- (void)dealloc{
[thePlace release];
[theVerb release];
[theNumber release];
[theStory release];
[theTemplate release];
[generateStory release];
[super dealloc];
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
小结:iOS中Xcode和Interface Builder联合应用 实例操作的内容介绍完了,希望本文对你有所帮助!!!