0

I am using this one in my application for adding UITextFields to my UITableViewCells, am now at a loss how I can let the user jump to the next item by using the next button in toolbar in keyboard,Here my code

 IBOutlet UITextField *contNo,*breed,*coat,*color,*birthdate;
 IBOutlet UIToolbar *toolBar;
 IBOutlet UIBarButtonItem *barButton;
 NSArray *fieldsArray;

 fieldsArray = [[NSArray alloc] initWithObjects:birthdate,contNo, breed,homeAddress, coat,color, nil];

-(IBAction)next
 { 
 for (int i=0; i<[fieldsArray count]; i++)
 { if ([[fieldsArray objectAtIndex:i] isEditing] && i!=[fieldsArray count]-1)
   {
         [[fieldsArray objectAtIndex:i+1] becomeFirstResponder];
      if (i+1==[fieldsArray count]-1)
        {
            [barButton setTitle:@"Done"];
            [barButton setStyle:UIBarButtonItemStyleDone];
        }else
        {
            [barButton setTitle:@"Done"];
            [barButton setStyle:UIBarButtonItemStyleBordered];
        }
        break;
            }
    }
 }

-(IBAction) previous
{
for (int i=0; i<[fieldsArray count]; i++)
 {
   if ([[fieldsArray objectAtIndex:i] isEditing] && i!=0)
    {
        [[fieldsArray objectAtIndex:i-1] becomeFirstResponder];
        [barButton setTitle:@"Done"];
        [barButton setStyle:UIBarButtonItemStyleBordered];
        break;
    }
  }
 } 

-(void)textFieldDidBeginEditing:(UITextField *)textField 
{
[textField setInputAccessoryView:toolBar];
for (int i=0; i<[fieldsArray count]; i++)
{
    if ([fieldsArray objectAtIndex:i]==textField)
    {
        if (i==[fieldsArray count]-1)
        {
            [barButton setStyle:UIBarButtonItemStyleDone];
        }
    }
  }
 }

When i try with the textfields outer the tabelview the next and previous working properly.The next and previous not working with the inside uitextfield inside the tabelview.Can any one pls help me to sort out

Community
  • 1
  • 1
fazil
  • 57
  • 1
  • 8

3 Answers3

0
#pragma mark - UITextField Delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{
    HRCFormCell * currentCell = (HRCFormCell *) textField.superview.superview;
    NSIndexPath * currentIndexPath = [self.tableview indexPathForCell:currentCell];

    if (currentIndexPath.row != [self.questionsArray count] - 1) {

        NSIndexPath * nextIndexPath = [NSIndexPath indexPathForRow:currentIndexPath.row + 1 inSection:0];
        HRCFormCell * nextCell = (HRCFormCell *) [self.tableview cellForRowAtIndexPath:nextIndexPath];

        [self.tableview scrollToRowAtIndexPath:nextIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 

        [nextCell.textField becomeFirstResponder];
    }

    return YES;
}

Please try following code may this help to you.

Nimit Parekh
  • 16,021
  • 8
  • 47
  • 70
0

add textfield's in contentview of tableview cell. set tags to textfield in cellforrowatindexpath such that tags increment as being added. tags can be of form "3+indexpath.row"

in text begin editing find the tag for current textfield, save the tag. in next ,increment tag,use tag to find textfield and make it first responder.

or try this link How to activate next UITextField in a UITableView? there's good answer provided by Fabiano Francesconi http://blog.encomiabile.it/2013/02/08/how-to-activate-next-uitextfield-in-uitableview-ios/

Community
  • 1
  • 1
Satyam Raikar
  • 439
  • 6
  • 19
0

This is an old post, but has a high page rank so I'll chime in with my solution.

I had a similar issue and ended up creating a subclass of UIToolbar to manage the next/previous/done functionality in a dynamic tableView with sections: https://github.com/jday001/DataEntryToolbar

You set the toolbar as inputAccessoryView of your text fields and add them to its dictionary. This allows you to cycle through them forwards and backwards, even with dynamic content. There are delegate methods if you want to trigger your own functionality when textField navigation happens, but you don't have to deal with managing any tags or first responder status.

There are code snippets & an example app at the GitHub link to help with the implementation details. You will need your own data model to keep track of the values inside the fields.

jday
  • 578
  • 5
  • 14