0
votes

I know that I need to tell my UITextField to resign first responder when I want to dismiss the keyboard, but I'm not sure how to know when the user has pressed the out side of view?

4

4 Answers

1
votes

You can achieve this by calling touch view delegate method provided as below.

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

Let me know in case of any concern.

1
votes

It will Hide Keyboard. i hope it will work for you.

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

}
0
votes

You can try touchesEnded method

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

i hop it helps..

you can simply add UITapGestureRecognizer to your 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
}