iOS键盘退出方式
iOS键盘退出方式的方法,使用代码注销第一响应者,即注销点击TextField所响应的键盘弹出事件:
1 [textField resignFirstResponder];
要使用这个方法进行键盘退出处理,需要我们对特定的视图控件进行处理,使之响应该方法,例如:键盘Return键,非键盘及TextField之外的视图,特定的Button。
我们使用StoryBoard以及代码来演示:
1.创建StoryBoard视图:3个TextField,2个Button
2.拖线并使用代码实现退出键盘的方法
1 #import "ViewController.h" 2 // 添加关于TextField的协议 3 @interface ViewController ()<UITextFieldDelegate> 4 5 @property (weak, nonatomic) IBOutlet UITextField *writeField; 6 @property (weak, nonatomic) IBOutlet UITextField *showField; 7 @property (weak, nonatomic) IBOutlet UITextField *x3rdField; 8 9 @end 10 11 @implementation ViewController 12 13 - (void)viewDidLoad { 14 [super viewDidLoad]; 15 // 添加代理 16 self.writeField.delegate = self; 17 self.showField.delegate = self; 18 self.x3rdField.delegate = self; 19 } 20 21 // 拖线Button事件,点击Button回收键盘 22 - (IBAction)sendAction:(id)sender { 23 [self keyboardQuit]; 24 } 25 26 - (IBAction)showAction:(id)sender { 27 [self keyboardQuit]; 28 } 29 30 #pragma mark - keyboard quit: click return 31 - (BOOL)textFieldShouldReturn:(UITextField *)textField{ 32 [self keyboardQuit]; 33 return YES; 34 } 35 36 #pragma mark - keyboard quit: click view 37 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 38 [self keyboardQuit]; 39 } 40 41 // 注销第一响应者方法 42 - (void)keyboardQuit{ 43 [self.writeField resignFirstResponder]; 44 [self.showField resignFirstResponder]; 45 [self.x3rdField resignFirstResponder]; 46 } 47 48 - (void)didReceiveMemoryWarning { 49 [super didReceiveMemoryWarning]; 50 // Dispose of any resources that can be recreated. 51 } 52 53 @end 54 /* 55 * keyboard quit: click return 点击Return回收键盘 56 * keyboard quit: click view 点击其他区域回收键盘 57 */