在iwatch中触摸UITextField外部时关闭键盘

时间:2015-08-04 10:12:30

标签: ios objective-c iphone keyboard uitextfield

我知道当我想解雇键盘时,我需要告诉我的UITextField辞职第一响应者,但我不确定如何知道用户何时按下out side of view

4 个答案:

答案 0 :(得分:1)

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

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

如有任何疑虑,请与我联系。

答案 1 :(得分:1)

它会隐藏键盘。 我希望它对你有用。

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

}

答案 2 :(得分:0)

我跳起来帮助..

您只需将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
}

答案 3 :(得分:0)

您可以尝试使用touchesEnded方法

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