//得到屏幕的寬和高
CGRect rect=[[UIScreen mainScreen] bounds];
CGSize size = rect.size;
int screenWidth = size.width;
int screenHeight = size.height;
//創建label視圖
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 30)];
//設置顯示內容
label.text = @"雨松MOMO的程序世界";
//設置背景顏色
label.backgroundColor = [UIColor blueColor];
//設置文字顏色
label.textColor = [UIColor whiteColor];
//設置顯示位置居中
label.textAlignment = UITextAlignmentCenter;
//設置字體大小
label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20];
//創建按鈕
button = [UIButton buttonWithType:1];
//設置按鈕範圍
button.frame = CGRectMake(0, 40, screenWidth, 30);
//設置按鈕顯示內容
[button setTitle:@"這是一個按鈕" forState:UIControlStateNormal];
//設置按鈕顯示顏色
button.backgroundColor = [UIColor blackColor];
//設置按鈕改變後 綁定響應方法
[button addTarget:self action:@selector(ButtonPressed) forControlEvents:UIControlEventTouchUpInside];
點擊這個按鈕後進入下面這個方法,彈出一個dialog對話框。
- (void)ButtonPressed
{
//創建對話框
UIAlertView * alertA= [[UIAlertView alloc] initWithTitle:@"我的視圖" message:@"歡迎一起學習IPHONE開發" delegate:self cancelButtonTitle:@"確定" otherButtonTitles: nil];
//添加取消按鈕
[alertA addButtonWithTitle:@"取消"];
//將這個UIAlerView 顯示出來
[alertA show];
//objective-C 不像java 有自己的垃圾回收機制 所以我們在編寫程序中一定要注意釋放內存 從一開始就養成良好習慣
[alertA release];
}
//創建進度條
slider=[[UISlider alloc] initWithFrame:CGRectMake(0,80,screenWidth,30)];
//進度條最大值
slider.maximumValue=100;
//進度條最小值
slider.minimumValue=0;
//起始點的位置
slider.value=20;
//設置背景顏色
slider.backgroundColor=[UIColor blackColor];
//設置進度條改變後 綁定響應方法
[slider addTarget:self action:@selector(valueChangeTest) forControlEvents:UIControlEventValueChanged];
拖動進度條後發生改變進入下面方法,[slider vale]可以得到拖動的進度值。
- (void)valueChangeTest
{
float value = [slider value];
NSLog(@"進度條已經發生改變:%f",value);
}
//創建文字輸入框
textfield = [[UITextField alloc] initWithFrame:CGRectMake(0,120,screenWidth,50)];
//默認顯示文字
textfield.text = @"這是一個輸入框";
//點擊後顯示文字
textfield.placeholder = @"請在輸入框是輸入信息";
//文字顯示位置,這裏居左對齊
textfield.textAlignment = UITextAlignmentLeft;
//默認顯示文字顏色
textfield.textColor = [UIColor grayColor];
//設置輸入的字體
textfield.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:0] size:17];
//設置輸入框的類型,3爲普通類型
textfield.borderStyle = 3;
//點擊輸入框後清楚原始內容
textfield.clearsOnBeginEditing = YES;
//設置輸入框背景顏色
textfield.backgroundColor = [UIColor blackColor];
//創建圖片視圖
imageview = [[UIImageView alloc] initWithFrame:
CGRectMake(100, 200, 120, 120)];
//設置圖片的顯示的資源路徑
[imageview setImage:[UIImage imageNamed:@"temp.jpg"]];
//創建一個隱藏的按鈕
backgroudButton=[[UIButton alloc] init];
//讓這個填充整個屏幕
backgroudButton.frame = self.view.frame;
//添加按鈕的響應時間,用來關閉軟鍵盤
[backgroudButton addTarget:self action:@selector(ButtonClick) forControlEvents:UIControlEventTouchUpInside];
-(void)ButtonClick
{
// 觸摸屏幕人以地方 關閉軟鍵盤
[textfield resignFirstResponder];
}
//將所有對象添加入視圖中
[self.view addSubview:backgroudButton];
[self.view addSubview:label];
[self.view addSubview:imageview];
[self.view addSubview:button];
[self.view addSubview:slider];
[self.view addSubview:textfield];
//釋放所有對象
[imageview release];
[label release];
[slider release];
[textfield release];
#import <UIKit/UIKit.h>
@interface HelloWorldViewController : UIViewController
{
//文本框
UILabel * label;
//按鈕
UIButton * button;
//進度條
UISlider *slider;
//輸入框
UITextField * textfield;
//圖片視圖
UIImageView *imageview ;
//背景按鈕
UIButton * backgroudButton;
}
@end
#import "HelloWorldViewController.h"
@implementation HelloWorldViewController
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
//得到屏幕的寬和高
CGRect rect=[[UIScreen mainScreen] bounds];
CGSize size = rect.size;
int screenWidth = size.width;
int screenHeight = size.height;
//創建label視圖
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 30)];
//設置顯示內容
label.text = @"雨松MOMO的程序世界";
//設置背景顏色
label.backgroundColor = [UIColor blueColor];
//設置文字顏色
label.textColor = [UIColor whiteColor];
//設置顯示位置居中
label.textAlignment = UITextAlignmentCenter;
//設置字體大小
label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20];
//創建按鈕
button = [UIButton buttonWithType:1];
//設置按鈕範圍
button.frame = CGRectMake(0, 40, screenWidth, 30);
//設置按鈕顯示內容
[button setTitle:@"這是一個按鈕" forState:UIControlStateNormal];
//設置按鈕顯示顏色
button.backgroundColor = [UIColor blackColor];
//設置按鈕改變後 綁定響應方法
[button addTarget:self action:@selector(ButtonPressed) forControlEvents:UIControlEventTouchUpInside];
//創建進度條
slider=[[UISlider alloc] initWithFrame:CGRectMake(0,80,screenWidth,30)];
//進度條最大值
slider.maximumValue=100;
//進度條最小值
slider.minimumValue=0;
//起始點的位置
slider.value=20;
//設置背景顏色
slider.backgroundColor=[UIColor blackColor];
//設置進度條改變後 綁定響應方法
[slider addTarget:self action:@selector(valueChangeTest) forControlEvents:UIControlEventValueChanged];
//創建文字輸入框
textfield = [[UITextField alloc] initWithFrame:CGRectMake(0,120,screenWidth,50)];
//默認顯示文字
textfield.text = @"這是一個輸入框";
//點擊後顯示文字
textfield.placeholder = @"請在輸入框是輸入信息";
//文字顯示位置,這裏居左對齊
textfield.textAlignment = UITextAlignmentLeft;
//默認顯示文字顏色
textfield.textColor = [UIColor grayColor];
//設置輸入的字體
textfield.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:0] size:17];
//設置輸入框的類型,3爲普通類型
textfield.borderStyle = 3;
//點擊輸入框後清楚原始內容
textfield.clearsOnBeginEditing = YES;
//設置輸入框背景顏色
textfield.backgroundColor = [UIColor blackColor];
//創建圖片視圖
imageview = [[UIImageView alloc] initWithFrame:
CGRectMake(100, 200, 120, 120)];
//設置圖片的顯示的資源路徑
[imageview setImage:[UIImage imageNamed:@"temp.jpg"]];
//創建一個隱藏的按鈕
backgroudButton=[[UIButton alloc] init];
//讓這個填充整個屏幕
backgroudButton.frame = self.view.frame;
//添加按鈕的響應時間,用來關閉軟鍵盤
[backgroudButton addTarget:self action:@selector(ButtonClick) forControlEvents:UIControlEventTouchUpInside];
//設置整個視圖的背景顏色
[self.view setBackgroundColor:[UIColor blackColor]];
//將所有對象添加入視圖中
[self.view addSubview:backgroudButton];
[self.view addSubview:label];
[self.view addSubview:imageview];
[self.view addSubview:button];
[self.view addSubview:slider];
[self.view addSubview:textfield];
//釋放所有對象
[imageview release];
[label release];
[slider release];
[textfield release];
}
- (void)ButtonPressed
{
//創建對話框
UIAlertView * alertA= [[UIAlertView alloc] initWithTitle:@"我的視圖" message:@"歡迎一起學習IPHONE開發" delegate:self cancelButtonTitle:@"確定" otherButtonTitles: nil];
//添加取消按鈕
[alertA addButtonWithTitle:@"取消"];
//將這個UIAlerView 顯示出來
[alertA show];
//objective-C 不像java 有自己的垃圾回收機制 所以我們在編寫程序中一定要注意釋放內存 從一開始就養成良好習慣
[alertA release];
}
- (void)valueChangeTest
{
float value = [slider value];
NSLog(@"進度條已經發生改變:%f",value);
}
-(void)ButtonClick
{
// 觸摸屏幕人以地方 關閉軟鍵盤
[textfield resignFirstResponder];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end