Reputation: 562
Before you dismiss my question as being a duplicate of this one: iphone, dismiss keyboard when touching outside of UITextField The issue for me is that my textfield is part of my prototype cell which has its own subclass of uitableview cell and so I'm not sure how to reference the textfield when trying to resign the first responder. SO, I can't just do this:
-(void)dismissKeyboard {
[aTextField resignFirstResponder];
}
How would I get across this situation?
Thanks
Upvotes: 0
Views: 963
Reputation: 10172
If I got you right you want to resign keyboard wile tapping on outSide of textfield
but you don't have reference of your textfield
.
Try this;
reftextField
Now in textFieldDidBeginEditing
set referenced text field to
- (void) textFieldDidBeginEditing:(UITextField *)textField{
reftextField = textField;
}
Now you can happily use on any button clock, (adding a transparent button on begin editing recomended)
- (void)dismissKeyboard {
[reftextField resignFirstResponder];
}
Or for resigning done button try this.
//for resigning on done button
- (BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
Upvotes: 0
Reputation: 42977
I think dismissing keyboard on tapping outside the table view cell is not good. User may accidently touching outside or he can scroll while entering text right?. You can use the UITextFieldDelegate
to dissmiss keyboard
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder]
}
Upvotes: 0
Reputation: 247
Try this code, it's very useful:
-(void)touchesBegan :(NSSet *)touches withEvent:(UIEvent *)event
{
[aTextField resignFirstResponder];
[super touchesBegan:touches withEvent:event];
}
Upvotes: 1