⼀、UITextField編程
UITextField(輸⼊框):是控制⽂本輸⼊和顯⽰的控件app
UITextField核⼼功能主要包含3個⽅⾯: ⽂本顯⽰ 輸⼊控制 外觀配置dom
一、⽂本顯⽰ide
// textField.text = @"你好";函數
// textField.textAlignment = NSTextAlignmentCenter;atom
textField.textColor = [UIColor blueColor];spa
textField.placeholder = @"請輸入我愛編程";.net
textField.font = [UIFont fontWithName:@"" size:20];代理
二、輸⼊控制orm
textField.enabled = YES;
textField.clearsOnBeginEditing = NO;
textField.secureTextEntry = YES;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDefault;
//自定義鍵盤視圖
UIView * inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 180)];
inputView.backgroundColor = [UIColor blueColor];
textField.inputView = inputView;
[inputView release];
//自定義鍵盤輔助視圖
UIView * inputAccessaryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 60)];
inputAccessaryView.backgroundColor = [UIColor yellowColor];
textField.inputAccessoryView = inputAccessaryView;
[inputAccessaryView release];
三、外觀控制
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
//設置文本框左視圖
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 40, 40);
[button setBackgroundImage:[UIImage imageNamed:@"face_monkey_2_24px_541648_easyicon.net.png"] forState:UIControlStateNormal];
textField.leftView = button;
textField.leftViewMode = UITextFieldViewModeAlways;
2、UIButton
一、UIButton的狀態
normal(普通狀態)
默認狀況
對應的枚舉常量:UIControlStateNormal
highlighted(高亮狀態)
按鈕被按下去的時候(手指還未鬆開)
對應的枚舉常量:UIControlStateHighlighted
disabled(失效狀態,不可用狀態)
若是enabled屬性爲NO,就是處於disable狀態,表明按鈕不能夠被點擊
對應的枚舉常量:UIControlStateDisabled
UIButton有不少種狀態,它提供了一些便捷屬性,能夠直接獲取當前狀態下的文字、文字顏色、圖片等
@property(nonatomic,readonly,retain) NSString *currentTitle;
@property(nonatomic,readonly,retain) UIColor *currentTitleColor;
@property(nonatomic,readonly,retain) UIImage *currentImage;
@property(nonatomic,readonly,retain) UIImage *currentBackgroundImage;
UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
// button.backgroundColor = [UIColor blackColor];
button.frame = CGRectMake(30, 100, 50, 50);
// button.layer.cornerRadius = 20;
//設置前景圖片
[button setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
//設置背景圖片
//設置普通狀態下的背景圖片
[button setBackgroundImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
//設置高亮狀態下的背景圖片
[button setBackgroundImage:[UIImage imageNamed:@"hilighted.png"] forState:UIControlStateHighlighted];
//添加事件
[button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
//設置按鈕title
[button setTitle:@"綠鳥" forState:UIControlStateNormal];
[button setTitle:@"藍鳥" forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- (void)clickButton:(UIButton *)button
{
self.window.backgroundColor = [UIColor colorWithRed:random()%100/100.0 green:random()%100/100.0 blue:random()%100/100.0 alpha:1];
button.backgroundColor = [UIColor whiteColor];
}
3、AppDelegate
****鍵盤收回****
一、將AppDelegate做爲UITextField的delegate
@interface AppDelegate : UIResponder <UIApplicationDelegate, UITextFieldDelegate>
二、AppDelegate.m⽂件實現textFieldShouldReturn:⽅法
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField becomeFirstResponder];
NSLog(@"%s", __FUNCTION__);
}// became first responder
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"%s", __FUNCTION__); // 輸出方法名
[textField resignFirstResponder];
return YES;
}// called when 'return' key pressed. return NO to ignore.
三、添加代理
textField.delegate = self;
****觸摸鍵盤外空白區域隱藏鍵盤****
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if (![touch.view isKindOfClass: [UITextField class]] || ![touch.view isKindOfClass: [UITextView class]]) {
[self.window endEditing:YES];
}
[super touchesBegan:touches withEvent:event];
}
4、iOS程序啓動流程
一、程序入口
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
UIApplicationMain剖析:
argc: argv[].count
argv[]: 存放的是全部的參數
principalClassName:若是是nil,UIApplicationMain()函數會給系統建立一個UIApplication這個類的一個對象,並且是惟一的,叫作應用程序對象
delegateClassName:UIApplicationMain()使用給定類建立代理並給應用程序設置代理
UIApplicationMain()這個函數,開啓事件循環
二、UIApplicationDelegate
UIApplicationDelegate是⼀個OC的協議。⾥⾯聲明瞭⼀堆⽅法,這些⽅ 法都與應⽤程序運⾏狀態有關,它們由應⽤程序代理實現。UIApplication 對象負責調⽤。
iOS生命週期:
UIApplicationDelegate
程序啓動:
application:didFinishLaunchingWithOptions:
applicationDidBecomeActive:
掛起:
applicationWillResignActive: (tag:按兩下Home鍵只發送此消息)
applicationDidEnterBackground:
恢復:
applicationWillEnterForeground:
applicationDidBecomeActive: (tag:今後狀態返回程序只發送此消息)
結束:
applicationWillResignActive:
applicationDidEnterBackground:
AppDelegate applicationWillTerminate:
輸出函數名:
NSLog(@"%s", __FUNCTION__);