UI開發----基礎視圖和程式啟動流程(delegate)

來源:互聯網
上載者:User

標籤:uibutton   ui   控制項   uiimage   uitextfield   

  //  Create by 郭仔  2015年04月13日22:10:23

今天介紹的基礎視圖包括:UITextField、UIButton

一.定義UITextField:

建立UITextField與建立UILabel的步驟很相似。?

 1、開闢空間並初始化(如果本類有初始化?方法,使?用?自?己的;否則 使?用?父類的)。?
2、設定?文本顯?示、輸?入相關的屬性?
3、添加到?父視圖上,?用以顯?示?
4、釋放

UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, 100, 50)];    textField.backgroundColor = [UIColor cyanColor];    [self.window addSubview:textField];    [textField release];
常用屬性和方法:

文本顯示:


輸入控制:


外觀控制:


代碼實現:

textField.text = @"Hello 郭仔";    textField.textColor = [UIColor redColor];    textField.textAlignment = NSTextAlignmentCenter;    // textField.font = [UIFont systemFontOfSize:12];    textField.font = [UIFont fontWithName:@"HelveticaBold" size:12];    textField.placeholder = @"請輸入使用者名稱";    // textField.enabled = NO;    textField.clearsOnBeginEditing = YES;    textField.secureTextEntry = YES;    // textField.keyboardType = UIKeyboardTypeNumberPad;    textField.returnKeyType = UIReturnKeyNext;    // textField.inputView = self.window;

    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(20, 50, 50, 30)];    view.backgroundColor = [UIColor greenColor];            //銀行安全輸入密碼是變換鍵盤,view是自己添加的鍵盤    // textField.inputView = view;    textField.inputAccessoryView = view;    [view release];

textField.borderStyle = UITextBorderStyleRoundedRect;    textField.clearButtonMode = UITextFieldViewModeWhileEditing;            UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];    leftView.backgroundColor = [UIColor blueColor];            textField.leftView = leftView;    textField.leftViewMode = UITextFieldViewModeAlways;        UIView * rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];    rightView.backgroundColor = [UIColor redColor];          textField.rightView = rightView;    textField.rightViewMode = UITextFieldViewModeAlways;        // =================================================    // 設定自身為代理    textField.delegate = self;    // 設定textfield為第一響應者,直接彈出鍵盤,游標直接定位到了textField中    [textField becomeFirstResponder];    // =================================================    [leftView release];    [rightView release];
==================================================================


擷取系統中字型名稱:

    // 列印出系統字型    NSArray *array = [UIFont familyNames];    for (NSString *s in array) {        NSLog(@"%@",s);    }
==================================================================
UIButton:

UIButton的使用:

建立UIButton與建立UILabel、UITextField、UIView的步驟很相似。? 1、建立button對象(如果本類有初始化?方法,使?用?自?己的;否則使?用 ?父類的)。?
2、設定按鈕顯?示相關的屬性?
3、為按鈕添加點擊事件? 4、添加按鈕到?父視圖上,?用以顯?示? 5、按鈕?無需釋放(因為使?用的是類?方法建立的button)

建立UIButton:

UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];    btn.frame = CGRectMake(100, 150, 100, 60);    btn.backgroundColor = [UIColor greenColor];    [btn setTitle:@"登陸" forState:UIControlStateNormal];    // [btn setTitle:@"登陸" forState:UIControlStateHighlighted];    // 添加button事件    [btn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn];
UIButton從?父類UIControl那繼承了控制相關的?方法,?比如添加事
件、移除事件等。
注:UIButton : UIControl : UIView

UIButton添加事件:



外觀控制:



代碼實現:

UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];    btn.frame = CGRectMake(100, 150, 100, 60);    btn.backgroundColor = [UIColor greenColor];    [btn setTitle:@"登陸" forState:UIControlStateNormal];    // [btn setTitle:@"登陸" forState:UIControlStateHighlighted];    // 添加button事件    [btn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];    // 移除button事件    // [btn removeTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];    [btn setTitle:@"取消" forState:UIControlStateHighlighted];        UIImage * img = [UIImage imageNamed:@"3.png"];    //[btn setImage:img forState:UIControlStateNormal];    UIImage * img2 =[UIImage imageNamed:@"4.png"];    //[btn setImage:img2 forState:UIControlStateHighlighted];        // 把圖片設定成背景    [btn setBackgroundImage:img forState:UIControlStateNormal];    [btn setBackgroundImage:img2 forState:UIControlStateHighlighted];    


===========================================================================

delegate:

點return回收鍵盤:

點擊鍵盤return按鈕實現鍵盤迴收的步驟: 1、將AppDelete作為UITextField的delegate 2、AppDelete.h?檔案接受UITextFieldDelegate協議 3、AppDelete.m?檔案實現textFieldShouldReturn:?方法。

代碼實現:

首先要遵守UITextFieldDelegate協議。

<span style="color:#333333;">    // 設定自身為代理    textField.delegate = self;    // 設定textfield為第一響應者,直接彈出鍵盤,游標直接定位到了textField中    [textField becomeFirstResponder];</span>

- (BOOL)textFieldShouldReturn:(UITextField *)textField{    // 取消第一響應者    [textField resignFirstResponder];    NSLog(@"你點擊了return按鈕");    return YES;}

========================================================================

IOS程式啟動流程:

UIApplicationMain:

UIApplicationMain在程式?入?口函數main函數中調?用,主要實現了3個
功能: 建立應?用程式(UIApplication)執行個體 建立應?用程式代理執行個體
建?立事件迴圈(runloop:死迴圈,不斷檢測程式運?行狀態,是否被觸 摸、晃動等)


 

int UIApplicationMain (!
int argc,!
char *argv[],!
NSString *principalClassName,!
NSString *delegateClassName!
);! 1、principalClassName:應?用程式對象的類名(UIApplication或其?子類)! 2、delegateClassName:應?用程式delegate的類名。(任何接受了 UIApplicationDelegate的類)! UIApplicationMain根據上述兩個類名建立應?用程式執行個體、應?用程式代理執行個體。
然後建?立事件迴圈(runloop),檢測程式的各種事件(程式開始啟動,接收到觸 摸等等);

==============

應用程式代理程式:

UIApplicationDelegate是?一個OC的協議。?裡?面聲明了?一堆?方法,這些?方
法都與應?用程式運?行狀態有關,它們由應?用程式代理實現。UIApplication 對象負責調?用。
UIApplicationDelegate中的定義的部分方法:


================================================================================

UIWindow:

UIWindow作為應?用程式的窗?口,在應?用程式啟動的時候就要載入,
各種UI界?面都是藉助window來呈現的。 UIWindow在建立的時候,應該和螢幕?一樣?大。通過[UIScreen
mainScreen].bounds可以獲得螢幕?大?小。
[self.window makeKeyAndVisible];//讓window變的可視,即顯?示 window。

================================================================================


UI開發----基礎視圖和程式啟動流程(delegate)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.