简体   繁体   English

使用UITextField和UITextView时textFieldShouldReturn方法的问题

[英]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 = @"";
}

In this link you will get solution of your problem. 此链接中,您将获得问题的解决方案。 In this given that how we hide keyboard when text editing done. 在此给出了我们如何在文本编辑完成时隐藏键盘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用UITextView时的性能问题 - Performance problem when using UITextView 如何向 UITextField & UITextView 添加方法? - How to add a method to UITextField & UITextView? 在UITextfield或UITextview中会触发哪种委托方法? - Which delegate method will get fired in UITextfield or UITextview? textFieldShouldReturn:方法问题 - textFieldShouldReturn: method issue 调用textFieldShouldReturn时会弹出popViewController吗? - popViewController when textFieldShouldReturn is called? 在iPhone的textFieldShouldReturn上使用resignFirstResponder时使用EXC_BAD_ACCESS - EXC_BAD_ACCESS when using resignFirstResponder on textFieldShouldReturn for iphone 有没有一种方法可以使UITextView具有类似UITextField框的类似周围阴影? - Is there a method to get a UITextView with similar surrounding shadow like a UITextField box? iPhone:切换视图时如何使 UITextField、UITextView 不可编辑 - iPhone: How to make a UITextField, UITextView uneditable when switching views 当UITextField和UITextview显示在UIWindow内部时,解散键盘 - Dissmissing the keyboard when UITextField and UITextview displayed inside of UIWindow 可以在不使用UITextField和UITextView iphone应用程序的情况下显示键盘吗? - It is possible to show keyboard without using UITextField and UITextView iphone app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM