Question

I have a grouped table view that contains 3 sections and each row per section. 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.

The two text fields have the returnKeyType as UIReturnKeyNext. For UITextView, the "return" button is present in keyboard to feed new line. So i used textFieldShouldReturn method to navigate to the next cell by pressing these return type buttons in 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. 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..

Was it helpful?

Solution

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.

OTHER TIPS

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:

- (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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top