我有一个奇怪的问题。我有3 UITextField
s和1 UITextView
。我可以通过以下方式从UITextField
移动到下一个UITextField
- (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;
}
一切正常,直到UITexView
,然后我的光标移动到文本视图的第二行,代替第一行文本视图。
文本视图来自IB。
文字视图的代码:
- (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;
}
看起来像这样:
可能是什么问题?为什么不从第一行文本视图开始?
答案 0 :(得分:29)
我敢打赌,如果textFieldShouldReturn:
是你的NO
,nextResponder
应该返回UITextField
。现在你总是返回YES
,这意味着密钥可能会传递给UITextView
,然后插入新行。
答案 1 :(得分:3)
对于任何人来寻找Swift 2.0翻译......
而不是在true
之后在textFieldShouldReturn
中返回yourUIITextView.becomeFirstResponder()
,而是返回false
。
为我工作!
希望这有帮助!