1. 程式人生 > >Objective-C語法之第一個iPhone應用程式的那些事兒(十)

Objective-C語法之第一個iPhone應用程式的那些事兒(十)

#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