I have a strange problem. I have 3 UITextField
s and one UITextView
. I can move from one UITextField
to next UITextField
by:
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
NSInteger nextTag = textField.tag + 1;
//-- try to find next responde
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder)
{
//-- found next responce ,so set it
[nextResponder becomeFirstResponder];
}
else
{
[scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
//-- not found remove keyboard
[textField resignFirstResponder];
return YES;
}
return YES;
}
Its all works fine until it comes to UITexView
, then my cursor moves at 2nd line of the text view in place of 1st line of text view.
Text view is taken from IB.
The code for text view:
- (void) textViewDidBeginEditing:(UITextView *)textView
{
optionalText.hidden = YES ;
[scrollView setContentOffset:CGPointMake(0, textView.center.y-100) animated:YES];
}
- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
// Any new character added is passed in as the "text" parameter
if ([text isEqualToString:@"\n"])
{
// Be sure to test for equality using the "isEqualToString" message
[textView resignFirstResponder];
[scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
// Return FALSE so that the final '\n' character doesn't get added
if ([[commentTxtView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0)
{
optionalText.hidden = NO ;
NSLog(@"Text View is null");
[scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
return FALSE;
}
return TRUE;
}
It looks like this:
What could be the problem? Why does it not start from 1st line of text view?
I bet your textFieldShouldReturn:
should return NO
in case the nextResponder
is your UITextField
. Right now you are always returning YES
, which means that the key probably being passed to the UITextView
, which will then insert the new line.
For Anybody coming in looking for Swift 2.0 translation...
Instead of returning true
in textFieldShouldReturn
after yourUIITextView.becomeFirstResponder()
, return false
.
Worked for me!
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With