0

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

Community
  • 1
  • 1
David West
  • 552
  • 2
  • 9
  • 21
  • You can if you add a `UITextField` property to your `UITableViewCell` subclass and wire this property up to the text field you have created in your prototype. – Scott Berrevoets Feb 25 '13 at 07:21
  • @Scott, I have already done that. How would I reference it, since this dismiss keyboard is part of my view controller class, not the uitableviewcell subclass. – David West Feb 25 '13 at 07:34
  • Check this response: http://stackoverflow.com/a/15160559/656600 – rptwsthi Mar 01 '13 at 14:53

3 Answers3

1

Try this code, it's very useful:

-(void)touchesBegan :(NSSet *)touches withEvent:(UIEvent *)event

{

    [aTextField resignFirstResponder];

    [super touchesBegan:touches withEvent:event];

}
San
  • 247
  • 2
  • 12
0

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]
 }
Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
  • Anil please read my question carefully. I'm trying to follow these instructions: http://stackoverflow.com/questions/5306240/iphone-dismiss-keyboard-when-touching-outside-of-textfield – David West Feb 25 '13 at 07:20
  • Hmm.. maybe but I need to do it based on my design. Can u help me with that? – David West Feb 25 '13 at 07:39
0

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;

  • Take global textField, lets call it 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;
    }
    
rptwsthi
  • 10,094
  • 10
  • 68
  • 109