Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 313
Confused Avatar asked Jul 26 '11 05:07

Confused


1 Answers

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.

like image 105
gamma Avatar answered Nov 14 '22 22:11

gamma