UPDATE:
I also tried implementing UITextViewDelegate delegate and then doing in my controller:
- (BOOL)textViewShouldEndEditing:(UITextView *)textVie
This is best way to add Done Button on keyboard
textField.returnKeyType = UIReturnKeyDone;
Ok, so I too have been struggling with this very issue. Currently I am accessing a UITextView in a UITableViewCell (via Tags). Because I am using prototype cells I cannot use IBActions nor IBOutlets like everyone suggests. Instead, I am using;
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
This will then provide me with the text each time the user hits a button. My solution then, was to get the ascii character for new line; "/n". If that is the text that was entered, I would resign the first responder. For example;
// If the text length is 0 then the user is deleting something, so only check the ascii character if there is text to check if (text.length != 0) { // Get the Ascii character int asciiCode = [text characterAtIndex:0]; // If the ascii code is /n or new line, then resign first responder if (asciiCode == 10) { [alertTextView resignFirstResponder]; [DeviceTextView resignFirstResponder]; } }
Not sure if anyone else will need this hacky solution but I figured I'd put it out there in case someone needs it!
Swift version:
In Your ViewController:
textField.returnKeyType = UIReturnKeyType.Done
textField.delegate = self
After your ViewController
extension MyViewController: UITextFieldDelegate {
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}