Kee Yen Yeo
Kee Yen Yeo

Reputation: 71

iOS Xcode keyboard done button function

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.

Upvotes: 7

Views: 23507

Answers (3)

skonb
skonb

Reputation: 436

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

Upvotes: 23

Luis
Luis

Reputation: 447

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;

}

Upvotes: 0

Josh Sherick
Josh Sherick

Reputation: 2161

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.

Upvotes: 1

Related Questions