同第一篇一样,这里我们还是同样构建一个一模一样的温度转换器。
1.新建Xcode工程
对于手动创建界面来说,模板对于我们来说不重要。这里方便讲解,继续使用和上一篇文章一样的Navigation-based模板,填写项目名称其他选项均默认。这里我新建工程名为HelloWorld2。
2.修改工程文件结构
新建的工程包含HelloWorld2AppDelegate.h/HelloWorld2AppDelegate.m RootViewController.h/RootViewController.m 以及两个xib文件。
第一步,将RootViewController对应的头和实现文件以及xib文件从项目中删除,然后在工程上点击右键选择“New File...”,选择Cocoa Touch选项卡里面的“UIViewController subclass”, next之后在subclass of 里面输入“UIViewController”(默认就是),继续“Next”,然后填写创建的子类名称,然后“Save”。
第二步,删掉HelloWorld2AppDelegate.h及实现.m文件。
第三步,找到HelloWorld2-Info.plist文件,删除叼Main nib file base name 字段保存。
做完这三步,文件修葺工作就完成了。如下图
3.手动创建界面
在你新建的视图控制类里面,有这样一个方法 叫viewDidLoad,看名字就知道当视图被加载的时候会调用次方法,大多数view的初始化工作都放在此方法里面。
根据我们之前在图纸上设计的界面,我们现在需要两个Label,两个输入框,一个按钮,以及按钮响应的方法。
第一步:点击上图中RootViewController.h,在里面定义两个UITextField控件,以及按钮响应的事件。
@interface RootViewController : UIViewController
{
IBOutlet UITextField *field1;
IBOutlet UITextField *field2;
}
- (IBAction)convert:(id)sender;
@end
第二步:打开RootViewController.m文件,找到ViewDidLoad方法,将如下代码粘贴在里面
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];//新建视图
contentView.backgroundColor = [UIColor whiteColor];
self.view = contentView;
[contentView release];
UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];//使用指定图片初始化UIImageView
bgImageView.userInteractionEnabled = YES;//交互
[self.view addSubview:bgImageView];//添加到视图
field1 = [[UITextField alloc] initWithFrame:CGRectMake(130.0, 39.0, 145.0, 31.0)];//初始化输入框
field1.keyboardType = UIKeyboardTypeNumbersAndPunctuation; //键盘类型
field1.borderStyle = UITextBorderStyleRoundedRect;
field1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
field2 = [[UITextField alloc] initWithFrame:CGRectMake(130.0, 82.0, 145.0, 31.0)];
field2.borderStyle = UITextBorderStyleRoundedRect;
field2.enabled = NO;
field2.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(42.0, 43.0, 90.0, 21.0)];//初始化UILabel
label1.text = @"华氏温度:";
label1.textColor = [UIColor greenColor];
label1.backgroundColor = [UIColor clearColor];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(42.0, 87.0, 90.0, 21.0)];
label2.text = @"摄氏温度:";
label2.textColor = [UIColor greenColor];
label2.backgroundColor = [UIColor clearColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];//初始化UIButton
btn.frame = CGRectMake(124.0, 142.0, 72.0, 37.0);
[btn setTitle:@"转换" forState:UIControlStateNormal];
[btn addTarget:self //设置点击响应事件
action:@selector(convert:)
forControlEvents:UIControlEventTouchUpInside];
//将以初始化的控件加入到视图
[bgImageView addSubview:field1];
[bgImageView addSubview:field2];
[bgImageView addSubview:label1];
[bgImageView addSubview:label2];
[bgImageView addSubview:btn];
[field1 release];
[field2 release];
[label1 release];
[label2 release];
//设置title
self.title = @"Converter";
第三步:新建委托类
点击main.m文件在里面输入如下代码
#import <UIKit/UIKit.h>
#import "RootViewController.h"
@interface HelloWorld2AppDelegate : NSObject <UIApplicationDelegate>
@end
@implementation HelloWorld2AppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
RootViewController *rootView = [[RootViewController alloc] init]; //新建视图
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootView];
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window addSubview:nav.view];
[window makeKeyAndVisible];
}
@end
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"HelloWorld2AppDelegate");
[pool release];
return retVal;
}
4.编译运行程序
这样,一个完全手动建立视图的温度转换器,新鲜出炉啦。虽然相比较自动累了点,但是你也了解了如何创建控件,如何在创建视图,如何将视图现实出来等等,也对Cocoa Touch的控件响应属性方法熟悉了点,对于新手来说,是不是一种收获呢?
其实还可以半自动半手动,比如创建控件的那部分完全可以自动完成,下篇文章将会分享一下如何半自动半手动的构建界面文件。
原创文章,转载请保留此字段出处:http://blog.youkuaiyun.com/everpenny/article/details/6892008