#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //让键盘称为第一响应者(进入当前的界面,键盘就弹出来) [_textF becomeFirstResponder]; _textF.delegate = self; //第一种:通过UITextFieldDelegate协议里面的方法 //第二种:点击除了键盘以外的任意位置让键盘隐藏 // (1) 手势 // UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOneTheView)]; // [self.view addGestureRecognizer:tap]; } -(void)tapOneTheView { [_textF resignFirstResponder]; } //第一种:通过UITextFieldDelegate协议里面的方法 -(BOOL)textFieldShouldReturn:(UITextField *)textField { return [_textF resignFirstResponder]; } //第三种 触碰事件 (触摸空白处) -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [_textF resignFirstResponder]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
时间: 2024-10-10 22:25:17