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

來源:互聯網
上載者:User
Objective-C文法之第一個iPhone應用程式的那些事兒
雨松MOMO原創文章如轉載,請註明:轉載至我的獨立網域名稱部落格雨松MOMO程式研究院,原文地址:http://www.xuanyusong.com/archives/432

Objective-C文法系列在之前的文章中一直在介紹基本的文法的相關知識,但是學習文法的目的還是為了走進iPhone IOS的開發世界。從今以後Objective-C文法文章將不在更新。全力更新IOS遊戲開發 軟體開發系列文章,這篇文章MOMO將帶各位盆友們簡單介紹iPhone開發的一些基本控制項的使用,簡單的構建我們第一個iPhone應用程式。各位盆友們我們先預熱一下,嘿嘿。

讀過我Android系列開發的盆友應該很清楚這個熟悉的介面吧,哇哢哢~~









擷取手機螢幕尺寸的方法

    //得到螢幕的寬和高    CGRect rect=[[UIScreen mainScreen] bounds];         CGSize size = rect.size;    int screenWidth = size.width;    int screenHeight = size.height;





1 .文字框視圖
在視圖中加入一個文字框,可在框內攥寫一些內容,設定字型顏色,位置 ,大小等等。
    //建立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];
2.按鈕視圖
       按鈕類型為1 為普通按鈕,CGrectMake設定按鈕的位置與大小,前兩個參數設定按鈕起始X 與 Y座標,後兩個參數設定按鈕的寬度與高度。
       這裡重點說一下addTarget, 它可以設定按鈕的綁定事件,action:設定按鈕點擊後回應程式法,這行代碼的意思是點擊這個按鈕後程式執行方法ButtonPressed這個函數中的代碼。
    //建立按鈕    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];   } 






3.進度條視圖
和上面button視圖的構建差不多,這裡設定進度條最大值與最小值,拖動的時候就可以直接得到這個範圍之間的數值,同樣將拖動事件綁定在valueChangeTest方法中。
    //建立進度條    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);    }










4.編輯框視圖
        非常常見的視圖,可以在編輯框中輸入資訊。前提是使用者觸摸點擊輸入框,這時彈出系統軟鍵盤方可輸入資訊,但是這個輸入框不會自動關閉,須要我們在程式中自己調用代碼去關閉,稍後介紹如何關閉這個輸入框。

    //建立文字輸入框    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];




5.圖片視圖 設定圖片在螢幕中顯示的位置,當然這個圖片檔案必需拷貝到工程當中,拷貝方法可以直接將圖片用滑鼠拖動到Xcode的工程中。
    //建立圖片視圖    imageview = [[UIImageView alloc] initWithFrame:                            CGRectMake(100, 200, 120, 120)];        //設定圖片的顯示的資源路徑    [imageview setImage:[UIImage imageNamed:@"temp.jpg"]];
6.透明全屏按鈕 它的存在就是為瞭解決IME出現後無法自動關閉,就是說如果IME軟鍵盤出現後 ,這時候點擊螢幕任意位置關閉IME,實現的原理是使用者點擊到了螢幕中設定的透明按鈕,調用關閉IME方法將IME關閉了而已 .
    //建立一個隱藏的按鈕    backgroudButton=[[UIButton alloc] init];    //讓這個填充整個螢幕    backgroudButton.frame = self.view.frame;     //添加按鈕的回應時間,用來關閉軟鍵盤    [backgroudButton addTarget:self action:@selector(ButtonClick) forControlEvents:UIControlEventTouchUpInside];  

點擊螢幕任意位置,關閉IME。

-(void)ButtonClick{     // 觸控螢幕幕人以地方 關閉軟鍵盤    [textfield resignFirstResponder];}


        這樣所有的視圖的代碼都已經貼上,這些視圖實際上是subView,須要將這些subView添加到螢幕的主視圖當中。並且為了避免記憶體出現泄漏,一定要及時的釋放這些視圖。
    //將所有對象添加入視圖中    [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];

下面給出完整的代碼

HelloWorldViewController.hController
#import <UIKit/UIKit.h>@interface HelloWorldViewController : UIViewController{    //文字框    UILabel * label;    //按鈕    UIButton * button;    //進度條    UISlider *slider;    //輸入框    UITextField * textfield;    //圖片視圖    UIImageView *imageview ;    //背景按鈕    UIButton * backgroudButton;}@end
HelloWorldViewController.m

#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


最後如果你還是覺得我寫的不夠詳細 看的不夠爽 不要緊我把原始碼的貼出來 歡迎大家一起討論學習雨松MOMO希望可以和大家一起進步。

:http://download.csdn.net/detail/xys289187120/3645156

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.