Problem with the textFieldShouldReturn method when using a UITextField with a UITextView

I have a grouped table view that contains 3 sections and each row in each section. The first two lines of sections contain a UITextField (Name and Subject are the names of sections), and the last contains a UITextView (Message is the section title), because I want to receive some data from the user by this controller.

Two text fields have returnKeyType as UIReturnKeyNext. For UITextView, a return button is present on the keyboard to feed a new line. Therefore, I used the textFieldShouldReturn method to go to the next cell by pressing these return type buttons in UIKeyboard.

The next button will work fine with the first text box (Name). Here the problem arises ... If I click the "Next" button of the second cell, it will go to the UITextView (last cell) with one row down. That is, the cursor moves one line separately from the starting position.

My code ...

-(BOOL) textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; if (textField == nameTextField) { [subjectTextField becomeFirstResponder]; } else if(textField == subjectTextField) { [messageTextView becomeFirstResponder]; } return YES; } 

What should I do to make this work beautiful? Thanks at Advance ..

+6
source share
3 answers

When testing many things, I found a simple but suitable solution:

 - (BOOL)textFieldShouldReturn:(UITextField *)textField { if ( [textField isEqual: nameTextField] ) { [nameTextField resignFirstResponder]; [messageTextView becomeFirstResponder]; return NO; } return YES; } 

It handles the resignation of the TextField name itself and returns NO in the request.

+11
source

Basically what happens is that when you return the return, you make the text view the first responder, and the return is added to the text view. That is why the cursor goes to the second line. Try to do this in your text element: delegation method:

 - (void)textViewDidChange:(UITextView *)textView { if(textView.text == @"\r") { textView.text = @""; } 
0
source

In this link you will receive a solution to your problem. In this case, how do we hide the keyboard when editing text.

-1
source

Source: https://habr.com/ru/post/893554/


All Articles