[英]Problem with textFieldShouldReturn method when using UITextField along with UITextView
I have a grouped table view that contains 3 sections and each row per section. 我有一个分组表视图,包含3个部分,每个部分每行。 The first two section rows contains UITextField(Name & Subject are the section titles) and the last one contains UITextView(Message is the section title) because i want to get some data from the user by this controller itself. 前两个部分行包含UITextField(名称和主题是部分标题),最后一个包含UITextView(消息是部分标题),因为我想通过此控制器本身从用户获取一些数据。
The two text fields have the returnKeyType as UIReturnKeyNext. 这两个文本字段的returnKeyType为UIReturnKeyNext。 For UITextView, the "return" button is present in keyboard to feed new line. 对于UITextView,键盘中存在“返回”按钮以提供新行。 So i used textFieldShouldReturn method to navigate to the next cell by pressing these return type buttons in UIKeyboard. 所以我使用textFieldShouldReturn方法通过在UIKeyboard中按下这些返回类型按钮来导航到下一个单元格。
The next button will work fine with the first text field(Name). 使用第一个文本字段(名称),下一个按钮可以正常工作。 Here the problem comes... If i click the Next button of second cell, It goes to the UITextView(last cell) with one line down. 这里出现问题...如果我单击第二个单元格的下一个按钮,它将转到UITextView(最后一个单元格),向下一行。 That is, the cursor moves one line apart from its original position. 也就是说,光标从其原始位置移开一行。
My code is... 我的代码是......
-(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 fine? 我应该怎么做才能使这项工作正常? Thanks in Advance.. 提前致谢..
While testing a lot of stuff I found a simple yet suitable solution: 在测试很多东西的同时,我找到了一个简单但合适的解决方案:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ( [textField isEqual: nameTextField] )
{
[nameTextField resignFirstResponder];
[messageTextView becomeFirstResponder];
return NO;
}
return YES;
}
It handles the resigning of the nameTextField itself and returns NO to the request. 它处理nameTextField本身的重新签名,并向请求返回NO。
Basically what is happening is that when return is tapped you make text view the first responder and the return gets added to the text view. 基本上发生的事情是,当点击返回时,您将文本视图作为第一个响应者,并将返回添加到文本视图中。 Thats why the cursor goes to the second line. 这就是光标到达第二行的原因。 Try doing this in your textViewDidChange: delegate method: 尝试在textViewDidChange:delegate方法中执行此操作:
- (void)textViewDidChange:(UITextView *)textView {
if(textView.text == @"\r") {
textView.text = @"";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.