//移除通知
- (void)dealloc {
[[NSNotificationCenter defaultCenter]removeObserver:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//添加tap手势
self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidesKeyboard)];
self.tapGesture.enabled = NO;//最开始手势设为no
[self.view addGestureRecognizer:self.tapGesture];
//键盘显示我用了通知结合手势来的
//注册通知,
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
}
//隐藏键盘
- (void)hidesKeyboard{
[self.view endEditing:YES];
self.tapGesture.enabled = NO;
}
// 完成通知的方法就是把手势设为yes即可
- (void)keyboardWillShow {
self.tapGesture.enabled = YES;
}
时间: 2024-10-05 10:22:31