我知道当我想关闭键盘时,我需要告诉我的UITextField放弃第一响应者,但是我不知道如何知道用户何时按下了out side of view?
out side of view
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]; }
如果有任何问题,请告诉我。
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]; }
fcg9iug33#
您可以尝试touchesEnded方法
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [yourTextfield resignFirstResponder]; }
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 }
4条答案
按热度按时间q9yhzks01#
您可以通过调用下面提供的触摸视图委托方法来实现此目的。
如果有任何问题,请告诉我。
2j4z5cfb2#
它会隐藏键盘。我希望它能为你工作。
fcg9iug33#
您可以尝试touchesEnded方法
jjhzyzn04#
我希望这能帮上忙。
您只需将UITapGestureRecognizer添加到您自己。