Ios 在iwatch中触摸UITextField外部时关闭键盘
我知道当我想关闭键盘时,我需要告诉我的UITextField让第一响应者辞职,但我不知道如何知道用户何时按下了视图外侧的Ios 在iwatch中触摸UITextField外部时关闭键盘,ios,objective-c,iphone,keyboard,uitextfield,Ios,Objective C,Iphone,Keyboard,Uitextfield,我知道当我想关闭键盘时,我需要告诉我的UITextField让第一响应者辞职,但我不知道如何知道用户何时按下了视图外侧的?我希望它能有所帮助 您只需将UITapGestureRecognizer添加到您的self.view中即可 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(taped:)]; [tap setNumberOfTapsRequir
?我希望它能有所帮助
您只需将UITapGestureRecognizer添加到您的self.view中即可
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(taped:)];
[tap setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:tap];
-(void)taped:(UITapGestureRecognizer*)gesture
{
//resign first responder
}
你可以尝试触碰触碰的方法
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[yourTextfield resignFirstResponder];
}
您可以通过调用下面提供的touch view delegate方法来实现这一点
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if (![[touch view] isKindOfClass:[UITextField class]]) {
[self.view endEditing:YES];
}
[super touchesBegan:touches withEvent:event];
}
如果有任何问题,请告诉我。它将隐藏键盘。
我希望它对你有用
UIGestureRecognizer *tapper;
- (void)viewDidLoad {
[super viewDidLoad];
tapper = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTap:)];
tapper.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapper];
}
- (void)handleSingleTap:(UIGestureRecognizer *)sender
{
[UIView beginAnimations:nil context:NULL];
[UIView commitAnimations];
[self.view endEditing:YES];
}
如果我点击其他UI对象或控件会发生什么?