How to add Done button to the keyboard?

Richard Knop picture Richard Knop · Apr 9, 2012 · Viewed 98.9k times · Source

UPDATE:

I also tried implementing UITextViewDelegate delegate and then doing in my controller:

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

I also set the delegate of the text view to be self (controller view instance).

Clicking the Done button still inserts just a new line :(


UPDATE:

What I have done so far. I have implemented a UITextFieldDelegate by my view controller.

I have connected the text view to the view controller via outlet.

Then I did:


self.myTextView.delegate = self;

And:

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

But when I click on Done button, it just adds a new line.

So I have a UITextView element on my scene and when a user taps it, keyboard appears and it can be edited.

However, I cannot dismiss the keyboard.

How can I add Done button to the keyboard so it can be dismissed?

Answer

Sebastian Flückiger picture Sebastian Flückiger · Apr 9, 2012

thats quite simple :)

[textField setReturnKeyType:UIReturnKeyDone];

for dismissing the keyboard implement the <UITextFieldDelegate> protocol in your class, set

textfield.delegate = self;

and use

- (void)textFieldDidEndEditing:(UITextField *)textField {
    [textField resignFirstResponder];
}

or

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