How to add the Finish button to the keyboard?

UPDATE:

I also tried implementing the UITextViewDelegate delegate, and then executed in my controller:

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

I also assigned a delegate to the text view self (controller instance).

Pressing the "Finish" button still inserts only a new line: (




UPDATE:

What I have done so far. I executed UITextFieldDelegate using my view controller.

I connected the text view to the view controller through the output.

Then I did:




 self.myTextView.delegate = self; 

and

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

But when I click Finish, it just adds a new line.

So, I have a UITextView element in my scene, and when the user picks it up, a keyboard appears and can be edited.

However, I cannot remove the keyboard.

How can I add a Done button to the keyboard so that it can be rejected?

+45
ios objective-c iphone xcode programmatically-created
Apr 09 2018-12-12T00:
source share
8 answers

It's pretty simple :)

 [textField setReturnKeyType:UIReturnKeyDone]; 

to reject the keyboard implements 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; } 
+107
Apr 09 '12 at 17:33
source share

Go to your storyboard, select your text box, and the "return key" option will appear in the "Attributes inspector" section ... select "Done".

Then go to your ViewController and add:

 - (IBAction)dismissKeyboard:(id)sender; { [textField becomeFirstResponder]; [textField resignFirstResponder]; } 

Then go back to your text box, click the exits and the "Finished on Exit" link to cancel the Keyboard action.

+13
Apr 09 2018-12-12T00:
source share

Add a UIToolBar as a custom view that will have a Done button as a UIBarButtonItem.

This is a safer and cleaner way to add a Finish button to any type of keyboard. Create a UIToolBar by adding a Done button to it and set the inputAccessoryView of any UITextField or UITextView.

 UIToolbar *ViewForDoneButtonOnKeyboard = [[UIToolbar alloc] init]; [ViewForDoneButtonOnKeyboard sizeToFit]; UIBarButtonItem *btnDoneOnKeyboard = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(doneBtnFromKeyboardClicked:)]; [ViewForDoneButtonOnKeyboard setItems:[NSArray arrayWithObjects:btnDoneOnKeyboard, nil]]; myTextField.inputAccessoryView = ViewForDoneButtonOnKeyboard; 

IBAction for the Finish button

  - (IBAction)doneBtnFromKeyboardClicked:(id)sender { NSLog(@"Done Button Clicked."); //Hide Keyboard by endEditing or Anything you want. [self.view endEditing:YES]; } 

SWIFT 3

 var ViewForDoneButtonOnKeyboard = UIToolbar() ViewForDoneButtonOnKeyboard.sizeToFit() var btnDoneOnKeyboard = UIBarButtonItem(title: "Done", style: .bordered, target: self, action: #selector(self.doneBtnFromKeyboardClicked)) ViewForDoneButtonOnKeyboard.items = [btnDoneOnKeyboard] myTextField.inputAccessoryView = ViewForDoneButtonOnKeyboard 

Function

  @IBAction func doneBtnFromKeyboardClicked (sender: Any) { print("Done Button Clicked.") //Hide Keyboard by endEditing or Anything you want. self.view.endEditing(true) } 
+10
Aug 19 '16 at 11:44
source share

Quick version:

In your ViewController:

 textField.returnKeyType = UIReturnKeyType.Done textField.delegate = self 

After viewing the ViewController

 extension MyViewController: UITextFieldDelegate { func textFieldShouldReturn(textField: UITextField) -> Bool { textField.resignFirstResponder() return true } } 
+9
Oct 06 '15 at 20:43
source share

Set the 'Return Key' option in the text field parameter in the inspector for the text field. Then right-click the text field and holding CTRL , select 'Did End On Exit' and drag it into the view controllers file. This will create an IBAction method. Give the method a name and enter the following values:

 [textField becomeFirstResponder]; [textField resignFirstResponder]; 

Your method should look like this:

 - (IBAction)methodName:(id)sender; { [sender becomeFirstResponder]; [sender resignFirstResponder]; } 
+1
Apr 04 '13 at 11:40
source share

Good, that's why I am also struggling with this very problem. I am currently accessing a UITextView in a UITableViewCell (via tags). Since I use prototypes, I cannot use IBActions or IBOutlets, as everyone says. Instead, I use:

- (BOOL) textView: (UITextView *) textView shouldChangeTextInRange: (NSRange) range replacementText: (NSString *) text

This will then give me text every time the user clicks a button. Then my solution was to get the ascii character for a new line; "/P". If this is the text that was entered, I will set aside 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 hacker solution, but I thought I put it there if someone needs it!

+1
Jan 30 '14 at 0:59
source share

This is the best way to add the Finish button on your keyboard.

 textField.returnKeyType = UIReturnKeyDone; 
0
Jul 04 '16 at 12:47
source share

In Interface Builder, you can set the type of the return button to Done using the textView properties.

Then you need to check if the back button is pressed with

  - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 

Check if the text == "/ n", then release the keyboard by canceling the first responder on your text element.

-2
Apr 09 2018-12-12T00:
source share



All Articles