首先建立一個基於視圖的項目,名字為ViewTest;
在ViewTestViewController.m檔案viewDidLoad方法中寫上如下代碼:
[cpp] //建立一個視圖UIView對象,擷取初始化螢幕大小,UIScreen mainScreen該裝置的內部螢幕,applicationFrame應用程式的螢幕面積幀點
UIView *view =
[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
//設定背景顏色:灰
view.backgroundColor = [UIColor lightGrayColor];
//建立標籤
CGRect frame = CGRectMake(10, 15, 300, 20);//設定標籤的位置及大小x y width height表示左上方和大小
UILabel *label = [[UILabel alloc] initWithFrame:frame]; //建立並初始化標籤,並且使大小是frame
label.textAlignment = UITextAlignmentCenter; //標籤中文字的對齊方向是置中
label.backgroundColor = [UIColor clearColor]; //標籤背景顏色透明的
label.font = [UIFont fontWithName:@"Verdana" size:20]; //標籤文字的Verdana體20號大小
label.text = @"This is a label";//標籤文字
label.tag = 1000;
//建立text
frame = CGRectMake(10, 70, 300, 30);
UITextField *text=[[UITextField alloc]initWithFrame:frame];
text.backgroundColor=[UIColor whiteColor];
text.text=@"我是任海麗";
//建立按鈕
frame = CGRectMake(10, 120, 300, 50);//按鈕位置大小
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setTitle:@"Click Me, Please!" forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
button.tag = 2000;
[button addTarget:self
action:@selector(buttonClicked:) //響應事件
forControlEvents:UIControlEventTouchUpInside];
//把標籤,文本和按鈕添加到view視圖裡面
[view addSubview:label];
[view addSubview:button];
[view addSubview:text];
self.view = view;
//建立一個視圖UIView對象,擷取初始化螢幕大小,UIScreen mainScreen該裝置的內部螢幕,applicationFrame應用程式的螢幕面積幀點
UIView *view =
[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
//設定背景顏色:灰
view.backgroundColor = [UIColor lightGrayColor];
//建立標籤
CGRect frame = CGRectMake(10, 15, 300, 20);//設定標籤的位置及大小x y width height表示左上方和大小
UILabel *label = [[UILabel alloc] initWithFrame:frame]; //建立並初始化標籤,並且使大小是frame
label.textAlignment = UITextAlignmentCenter; //標籤中文字的對齊方向是置中
label.backgroundColor = [UIColor clearColor]; //標籤背景顏色透明的
label.font = [UIFont fontWithName:@"Verdana" size:20]; //標籤文字的Verdana體20號大小
label.text = @"This is a label";//標籤文字
label.tag = 1000;
//建立text
frame = CGRectMake(10, 70, 300, 30);
UITextField *text=[[UITextField alloc]initWithFrame:frame];
text.backgroundColor=[UIColor whiteColor];
text.text=@"我是任海麗";
//建立按鈕 www.2cto.com
frame = CGRectMake(10, 120, 300, 50);//按鈕位置大小
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setTitle:@"Click Me, Please!" forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
button.tag = 2000;
[button addTarget:self
action:@selector(buttonClicked:) //響應事件
forControlEvents:UIControlEventTouchUpInside];
//把標籤,文本和按鈕添加到view視圖裡面
[view addSubview:label];
[view addSubview:button];
[view addSubview:text];
self.view = view;
事件回應程式法彈出一個警告框:
[html]-(IBAction) buttonClicked: (id) sender{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Action invoked!" message:@"Button clicked!"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
-(IBAction) buttonClicked: (id) sender{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Action invoked!" message:@"Button clicked!"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
給出:
什麼視圖介面,你都可以用代碼寫出來,並設定屬性,當然這個不是簡便的方法,沒有直接拖來的方便,不過這樣做是為了熟悉代碼並知道是怎麼實現的;每個控制項都有好多屬性,並且要明白是怎麼用的。學習過程 中不怕麻煩,希望跟大家一塊努力學習!有什麼不好的地方,請多指出!!!
摘自 任海麗(3G/移動開發)