UI第二天:第⼆講:基礎視圖、程序啓動流程

⼀、UITextField
UITextField(輸⼊框):是控制⽂本輸⼊和顯⽰的控件。
在App中UITextField 出現頻率也⽐較⾼。
iOS系統藉助虛擬鍵盤實現輸⼊,當點擊輸⼊框,系統會⾃動調出鍵盤,⽅便 你進⼀步操作。
在你不需要輸⼊的時候,可以使⽤收回鍵盤的⽅法,收回彈出的 鍵盤。
UITextField和UILabel相⽐,UILabel主要⽤於⽂字顯⽰,不能編輯, UITextField允許⽤戶編輯⽂字(輸⼊)。
創建UITextField與創建UILabel的步驟很相似。
1、開闢空間並初始化(如果本類有初始化⽅法,使⽤⾃⼰的;否則 使⽤⽗類的)。
2、設置⽂本顯⽰、輸⼊相關的屬性
3、添加到⽗視圖上,⽤以顯⽰
4、釋放
UITextField *userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 190, 30)];
userNameTextField.borderStyle = UITextBorderStyleRoundedRect;
userNameTextField.placeholder = @"⼿機號/郵箱";
[containerView addSubview:userNameTextField];
[userNameTextField release];
UITextField核⼼功能主要包含3個⽅⾯:
⽂本顯⽰
輸⼊控制
外觀配置
⽂本顯⽰


輸⼊控制


外觀控制



//初始化
   
UITextField *text= [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 50)];
   
//字體顏色
    text.
backgroundColor = [UIColor clearColor];
    text.
textColor = [UIColor tangerineColor];
   
// 對齊方式
    text.
textAlignment = NSTextAlignmentLeft;
   
//字體大小
    text.
font = [UIFont systemFontOfSize:20];
   
//佔位字
    text.
placeholder = @"請輸入";
   
//邊框格式
    text.
borderStyle = UITextBorderStyleRoundedRect;
   
//清楚按鈕 默認NO
    text.
clearButtonMode = UITextFieldViewModeWhileEditing;
   
// 是否允許輸入
    text.
enabled = YES;
   
// 是否開始輸入的時候清空輸入框內容
    text.
clearsOnBeginEditing = YES;
   
//密文輸入
    text.
secureTextEntry = YES;
   
// 彈出鍵盤類型
 
//   text.keyboardType = UIKeyboardTypeNumberPad;
   
// return設置
    text.
returnKeyType = UIReturnKeyNext;
//    UIView *inview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 300)];
 
//  inview.backgroundColor = [UIColor qianweise];
//    //自定義鍵盤
//    text.inputView = inview;
//   
//     UIView *inview1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
//    inview1.backgroundColor = [UIColor qianweise];
//    //輔助視圖
//    text.inputAccessoryView = inview1;
   
// [inview1 release];
   
   
//輸入左視圖
//    text.leftView = inview;
//    text.leftViewMode = UITextFieldViewModeAlways;
//    [inview release];
   
// 設置代理(方法在那裏實現的 就把誰設置爲代理)
    text.delegate = self;
// 實現協議的方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // 代理可以有很多個點那個那個就是textfield
    textField.backgroundColor = [UIColor colorWithRed:arc4random()%255/256.0 green:arc4random()%255/256.0 blue:arc4random()%255/256.0 alpha:1];
    // 鍵盤迴收 取消第一響應者
    [textField resignFirstResponder];
    return YES;
}
⼆、UIButton
UIButton(按鈕):是響應⽤戶點擊的控件。在App中UIButton是出 現頻率很⾼的控件。
UIButton與UILabel、UITextField側重點不同,側重於處理點按。當 然UIButton類也提供了⼀些⽅法控制按鈕外觀。
創建UIButton與創建UILabel、UITextField、UIView的步驟很相似。
1、創建button對象(如果本類有初始化⽅法,使⽤⾃⼰的;否則使⽤ ⽗類的)。
2、設置按鈕顯⽰相關的屬性
3、爲按鈕添加點擊事件
4、添加按鈕到⽗視圖上,⽤以顯⽰
5、按鈕⽆需釋放(因爲使⽤的是類⽅法創建的button)
UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
loginButton.frame = CGRectMake(30, 200, 60, 30);
[loginButton setTitle:@"登錄" forState:UIControlStateNormal];
[loginButton addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
[containerView addSubview:loginButton]
UIButton添加事件

外觀控制



UIView是所有可視化控件的基類。
UILabel、UITextField、UIButton是具有特定外觀特定功能的視圖。
UILabel側重於⽂本的呈現。
UITextField側重於輸⼊。
UIButton側重於點擊事件處理。
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
    b.
frame = CGRectMake(100, 100, 100, 100);
    b.
backgroundColor = [UIColor clearColor];
   
//設置標題
    [b
setTitle:@"普通" forState:(UIControlStateNormal)];
    [b
setTitle:@"高亮" forState:(UIControlStateHighlighted)];
    [b
setTitle:@"選中" forState:(UIControlStateSelected)];
    [b
setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [b
setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    [b
setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    [b
addTarget:self action:@selector(button:) forControlEvents:(UIControlEventTouchUpInside)];
   
// 如果不是png格式的圖片加上後綴
   
    [b
setBackgroundImage:[UIImage imageNamed:@"Normal.png"] forState:UIControlStateNormal];
    [b
setBackgroundImage:[UIImage imageNamed:@"Highlighted.png"] forState:UIControlStateHighlighted];
    [b
setBackgroundImage:[UIImage imageNamed:@"Selected.png"] forState:UIControlStateSelected];
    [self.window addSubview:b];
-(void)button:(UIButton *)button
{
    button.selected = !button.selected;
//    button.backgroundColor = [UIColor colorWithRed:arc4random()%255/256.0 green:arc4random()%255/256.0 blue:arc4random()%255/256.0 alpha:1];
}

三、delegate
點return回收鍵盤
點擊鍵盤return按鈕實現鍵盤迴收的步驟:
1、將AppDelete作爲UITextField的delegate
2、AppDelete.h⽂件接受UITextFieldDelegate協議
3、AppDelete.m⽂件實現textFieldShouldReturn:⽅法
AppDelegate.m
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{ [textField resignFirstResponder]; return YES; }
UITextField不應該在類內部(.m⽂件)實現textFieldShouldReturn:, 應爲有時候,我們點return的時候,並不總是想回收鍵盤,例如:有 兩個輸⼊框,第⼀個輸⼊框輸⼊完成之後,⽤戶點return按鈕,將光 標移動到第⼆個輸⼊框(即:第⼆個輸⼊框稱爲第⼀響應者)
對於⼀個V來說,⾃⼰只負責觸發事件,事件由外界實現,即 delegate。


四、程序啓動流程
main函數是程序⼊⼝
任何⼀個程序,⽆論是基於Mac OS還是iOS,程序都是從main.m⽂ 件的main函數開始執⾏的。
int main(int argc, char * argv[])
{
@autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
} 執⾏UIApplicationMain函數時做了跳轉,轉到了AppDelete中
UIApplicationMain
UIApplicationMain在程序⼊⼝函數main函數中調⽤,主要實現了3個 功能:
創建應⽤程序(UIApplication)實例
創建應⽤程序代理實例
建⽴事件循環(runloop:死循環,不斷檢測程序運⾏狀態,是否被觸 摸、晃動等)
UIApplicationMain剖析
int UIApplicationMain ( int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName );
1、principalClassName:應⽤程序對象的類名(UIApplication或其⼦類)
2、delegateClassName:應⽤程序delegate的類名。(任何接受了 UIApplicationDelegate的類)
UIApplicationMain根據上述兩個類名創建應⽤程序實例、應⽤程序代理實例。
然後建⽴事件循環(runloop),檢測程序的各種事件(程序開始啓動,接收到觸 摸等等)
應⽤程序代理
應⽤程序代理,主要檢測應⽤程序的狀態並做出相應的處理。
應⽤程序的狀態有很多,⽐如:程序啓動、進⼊活躍狀態、進到後 臺、內存警告、收到遠程消息等等
任何接受了UIApplicationDelegate協議的對象都可以成爲應⽤程序 代理。
⼀旦應⽤程序的某種狀態觸發,就會執⾏相應的代理⽅法。
UIApplicationDelegate
UIApplicationDelegate是⼀個OC的協議。
⾥⾯聲明瞭⼀堆⽅法,這些⽅ 法都與應⽤程序運⾏狀態有關,它們由應⽤程序代理實現。UIApplication 對象負責調⽤。
UIApplicationDelegate

在AppDelete.m各個代理⽅法⾥打印log,查看各個代理⽅法的 執⾏順序和執⾏順序。
NSLog(@"%s %d",__FUNCTION__,__LINE__);
- (void)applicationWillResignActive:(UIApplication *)application {
   
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
   
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
   
// _FUNCION_打印 調用了那個方法
   
// _LINE_ 打印 這個方法在多少行
   
NSLog(@"程序小退");
   
NSLog(@"%s %d", __FUNCTION__,__LINE__);
}

- (
void)applicationDidEnterBackground:(UIApplication *)application {
   
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
   
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
   
NSLog(@"程序進入後臺");
   
NSLog(@"%s %d", __FUNCTION__,__LINE__);
}

- (
void)applicationWillEnterForeground:(UIApplication *)application {
   
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
   
NSLog(@"程序進入前臺");
   
NSLog(@"%s %d", __FUNCTION__,__LINE__);
}

- (
void)applicationDidBecomeActive:(UIApplication *)application {
   
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    
NSLog(@"程序激活");
   
NSLog(@"%s %d", __FUNCTION__,__LINE__);
}

- (
void)applicationWillTerminate:(UIApplication *)application {
   
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
   
NSLog(@"程序意外退出");
   
NSLog(@"%s %d", __FUNCTION__,__LINE__);
   
// 意外退出
}

- (
void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
   
NSLog(@"內存警告是會觸發");
}
UIWindow
UIWindow作爲應⽤程序的窗⼝,在應⽤程序啓動的時候就要加載, 各種UI界⾯都是藉助window來呈現的。
UIWindow在創建的時候,應該和屏幕⼀樣⼤。通過[UIScreen mainScreen].bounds可以獲得屏幕⼤⼩。
[self.window makeKeyAndVisible];//讓window變的可視,即顯⽰ window。
總結
UILabel、UITextField、UIButton都是特別常⽤的控件。
他們都是直接或者間接繼承於UIView,只是外觀和功能不同。
熟悉各種控件的特點和⽅法是我們後期做項⺫的保障。
delegate是iOS重要的設計模式,理解它的原理,有助於寫出優質代碼 main函數是程序的⼊⼝。
UIApplicationMain函數創建了應⽤程序實例,應 ⽤程序代理實例,建⽴事件循環。

發佈了33 篇原創文章 · 獲贊 0 · 訪問量 7615
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章