Reputation:
So i've got a UITextField that is the first responder when the page is opened (works fine). Then I've got a UITextView that is becoming the first responder when the user has pressed return and that's where my problem is. When pressing return things seem to work fine with the first responders, but the UITextView adds a line and starts the cursor blinking on the second line.. Is there anyone who may be able to help me? Thanks in advance!
Here is the switch
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
[self.fieldDescription.textView becomeFirstResponder];
return YES;
}
See where the icon is blinking? That's where it starts when the first responders change...
Upvotes: 11
Views: 4109
Reputation: 12216
Great question and description. I had the exact same problem.
No answer on this thread solved it for me. Someone put a link in the comments (by @AndreiRadulescu) of another thread. an answer by @rmaddy on that thread solved it. His answer was:
One solution to solve this is when you implement the textFieldShouldReturn: delegate method, be sure to return NO, and not YES. By returning NO, the newline isn't passed on to the text field.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// move to next field or whatever you need
[myTextView becomeFirstResponder];
return NO;
}
In Swift:
//Dismisses keyboard upon tapping the return key
func textFieldShouldReturn(textField: UITextField) -> Bool {
txtTask.resignFirstResponder()
return false
}
Upvotes: 15
Reputation: 1034
I my case, I just set a blank text when textViewShouldBeginEditing
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
self.myTextView.text =@"";
return YES;
}
Upvotes: 0
Reputation: 41
Try using this
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self.fieldDescription becomeFirstResponder];
}
instead of
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
textFieldShouldReturn
--> automatically resigns first responder
Upvotes: 1
Reputation: 434
TextView has a delegate method which is to check the characters being typed by the user
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
using this you can stop the new line and return the keyboard, see the below code
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([textView.text isEqualToString:@"\n"])
{
[textView resignFirstResponder];
return NO;
}
return YES;
}
Upvotes: 2