7

I just can't make the "Done" button to quit the keyboard.

I used this in my controller.h file

- (IBAction)textFieldDoneEditing:(id)sender;

and this for my controller.m file

- (IBAction)textFieldDoneEditing:(id)sender {
  [sender resignFirstResponer];
}

and I'm mixed up in wiring the .xib part.

dandan78
  • 13,328
  • 13
  • 64
  • 78
Kee Yen Yeo
  • 71
  • 1
  • 1
  • 3
  • 1
    I think you must have forgot to set `delegate` for your textfield. – Kjuly Apr 06 '12 at 05:13
  • similar post on Stackoverflow, follow it. http://stackoverflow.com/questions/2828826/iphone-keyboard-done-button-and-resignfirstresponder – AsifHabib Nov 14 '12 at 06:50

3 Answers3

23

Make the controller a delegate of the UITextField/UITextView in IB or from code like textField.delegate = self;

Editted: For this you need to declare the controller a delegate of UITextFieldDelegate/UITextViewDelegate as

@interface Controller : <UITextFieldDelegate> { ...

, then override the method:

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

for UITextField and

-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
    [textView resignFirstResponder];
    return YES;
}

for UITextView

skonb
  • 436
  • 2
  • 4
1

In your .xib, right click on your text view, drag to "File's Owner", and click "delegate". Should work now?

Edit: Whoops, sorry I'm an idiot, do what that other guy says. If you don't know how to set the delegate in code though, you can do it my way in IB.

Josh Sherick
  • 2,161
  • 3
  • 20
  • 37
  • I set the delegate in code, but ALSO had to connect the text fields in IB as you suggest, so thanks for posting this answer. – Tony Adams Jul 31 '14 at 16:33
0

Let me make my first contribution: If you have multiple text fields, group them in a @property (strong, nonatomic)

*.h

@property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *collectingData;

*.m

-(BOOL)textFieldShouldReturn:(UITextField *)boxes
    {
        for (UITextField *boxes in collectingData) {
        [boxes resignFirstResponder];
    }
    return YES;

}
Luis
  • 447
  • 1
  • 7
  • 24