0

my code:

.h:

- (IBAction)backgroundTouched:(id)sender;

.m

- (IBAction)backgroundTouched:(id)sender {
    [textField resignFirstResponder];
    [self.view endEditing:YES];

    NSLog(@"backgound taped");
}

I dont know why the backgroundTouched not called when i tap the background and the keyboard not hidden. I think never called because of wiring up code problem. Neither Both textField resignFirstResponder and self.view endEditing:YES nor NSLog is working. Can anyone let me know how to do this? or What am i missing here? Im trying to hide the keyboard after done writing in uitextfield, the textfield is inside uitableview cell.

P.S i made it in uitableviewcontroller without xib file

Thank you.

dejoong
  • 2,213
  • 5
  • 25
  • 24

1 Answers1

1

Instead of a backgroundTouched method (which I have no idea how it gets actually called), consider using the delegate method for UITextField, namely textFieldDidEndEditing: (I've linked Apple's documentation for you).

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • i think textFieldDidEndEditing is called after the text field resigns its first responder. My problem is i dont kno where to resigns the text field if not use backgroundTouched. Or simply i dont know how to tell the app that im done with the keyboard. Please advice. – dejoong Apr 08 '12 at 07:19
  • if `textFieldDidEndEditing` is called, that means the user typed in the "done" button (or the "go" button or "search" button or whatever finishes up the text field), or the user touched outside of the text field. In any event, `textFieldDidEndEditing` gets called and *that's* where you should call your `resignFirstResponder` to tell the keyboard to go away. – Michael Dautermann Apr 08 '12 at 07:21
  • i put UITextFieldDelegate on the interface, put - (void) textFieldDidEndEditing:(UITextField *)textField on the implementation which call [textField resignFirstResponder] and put textField.delegate = self; on viewDidLoad. The keyboard still there etiher tap outside of the uitextfield or done button. What am i missing here? – dejoong Apr 08 '12 at 07:38
  • You probably are not setting the delegate at the right time. If the text field is part of a table view cell, set it during your `cellForRowAtIndexPath:` method. – Michael Dautermann Apr 08 '12 at 07:40
  • i only put [cell addSubview:textField]; inside cellForRowAtIndexPath, the init and any other setter i put in viewDidLoad. Anyway, instead of textFieldDidEndEditing i use "- (BOOL)textFieldShouldReturn:(UITextField *)textField" it worked with done button pressed but not with press anywhere outside the textfield. Since i used UIKeyboardTypeNumberPad the done button is not exist, so i really need tap anywhere to tell the keyboard must go away. I dont know why neither textFieldDidEndEditing nor (i tried) textFieldShouldEndEditing is working. Any idea? – dejoong Apr 08 '12 at 08:07
  • as you suggested textField.delegate = self; [cell addSubview:textField]; [textField release]; into cellForRowAtIndexPath, but still no luck – dejoong Apr 08 '12 at 08:15