鍵盤下落第一種方法:輸入框綁定方法
UITextField *textfield=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 40)];
textfield.borderStyle=UITextBorderStyleRoundedRect;
textfield.placeholder=@"第一種方法";
textfield.clearButtonMode=UITextFieldViewModeWhileEditing;
textfield.backgroundColor= [UIColor magentaColor];
//鍵盤下去第一種方法:輸入框綁定方法
//UIControlEventEditingDidEndOnExit編輯結束并退出
//點擊return鍵鍵盤下去綁定的方法中可以什么都不寫
[textfield addTarget:self action:@selector(keyboardDown) forControlEvents:UIControlEventEditingDidEndOnExit];
[self.view addSubview:textfield];
//鍵盤下落第二種方法:失去第一響應者
//創建一個control點擊control鍵盤下去
UIControl *control = [[[UIControl alloc] initWithFrame:[UIScreen mainScreen] bounds]:
[control addTarget:self action:@selector(controlTouched) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:control];
//創建一個輸入框
UITextField *textfield=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 40)];
//設置tag值,方便等下找到并針對這個輸入框的操作
textfield.tag = 30;
textfield.borderStyle=UITextBorderStyleRoundedRect;
textfield.placeholder=@"第二種方法";
textfield.clearButtonMode=UITextFieldViewModeWhileEditing;
textfield.backgroundColor= [UIColor magentaColor];
[self.view addSubview:textfield];
//程序運行就讓鍵盤上來這個時候輸入框第一響應者
//becomeFirstResponder成為第一響應者
[textfield becomeFirstResponder];
//實現control綁定的方法
-(void)controlTouched {
//讓輸入框失去第一響應者
UITextField *textfield=[self.view viewWithTag:30];
//resignFirstResponder失去第一響應者
[textfield resignFirstResponder];
}
//鍵盤下落第三種方法:touchesBegan
//創建一個輸入框
UITextField *textfield=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 40)];
textfield.borderStyle=UITextBorderStyleRoundedRect;
textfield.placeholder=@"第三種方法";
textfield.clearButtonMode=UITextFieldViewModeWhileEditing;
textfield.backgroundColor= [UIColor magentaColor];
[self.view addSubview:textfield];
//使用系統的觸摸方法,在觸摸開始的時候調用
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
//endEditing是否強制結束編輯
[self.view endEditing:YES];
}