ios 触摸iWatch中的UITextField外部时关闭键盘

v09wglhw  于 2022-11-26  发布在  iOS
关注(0)|答案(4)|浏览(409)

我知道当我想关闭键盘时,我需要告诉我的UITextField放弃第一响应者,但是我不知道如何知道用户何时按下了out side of view

q9yhzks0

q9yhzks01#

您可以通过调用下面提供的触摸视图委托方法来实现此目的。

- (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];
}

如果有任何问题,请告诉我。

2j4z5cfb

2j4z5cfb2#

它会隐藏键盘。我希望它能为你工作。

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];

}
fcg9iug3

fcg9iug33#

您可以尝试touchesEnded方法

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [yourTextfield resignFirstResponder];
}
jjhzyzn0

jjhzyzn04#

我希望这能帮上忙。
您只需将UITapGestureRecognizer添加到您自己。

UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(taped:)];
[tap setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:tap];

-(void)taped:(UITapGestureRecognizer*)gesture
{
    //resign first responder
}

相关问题