Richard Knop Richard Knop - 5 months ago 10
iOS Question

How to add Done button to the keyboard?

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

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;
}
Comments