Question

Newbie obj-c question.

I have a custom tableview with four custom cells. In every cell is a editable textfield for customer info. I need to improve switching between textfields by Input Accessory View. http://uaimage.com/image/62f08045

I created an NSMutableArray with capacity 4. I tagged textfields and added them in this array in text field delegate method:

- (BOOL) textFieldShouldReturn:(UITextField *)textField {
    FDTextFieldCell *cell = (FDTextFieldCell *)[textField superview];
    NSIndexPath *indexPath = [[self tableView] indexPathForCell:cell];

    if ([indexPath section] == 0) {
        if ([indexPath row] == 0) {
            [[FDProfile sharedProfile] setName:[textField text]];
            [textField setTag:1];
            [textFieldsArray insertObject:textField atIndex:0];
        } else if ([indexPath row] == 1) {
            [[FDProfile sharedProfile] setSurname:[textField text]];
            [textField setTag:2];
            [textFieldsArray insertObject:textField atIndex:1];
        } else if ([indexPath row] == 2) {
            [[FDProfile sharedProfile] setNickname:[textField text]];
            [textField setTag:3];
            [textFieldsArray insertObject:textField atIndex:2];
        } else if ([indexPath row] == 3) {
            [[FDProfile sharedProfile] setEmail:[textField text]];
            [textField setTag:4];
            [textFieldsArray insertObject:textField atIndex:3];
            [textField resignFirstResponder];
        }
    }

    [[self tableView] reloadData];
    return YES;
}

Now i try to improve functionality of button "Next":

- (void) inputAccessoryViewDidSelectNext:(FDInputAccessoryView *)view {
    for (UITextField *textField in [self textFieldsArray]) {
        if ([textField isFirstResponder]) {
     textField = [[self textFieldsArray] objectAtIndex:textField.tag + 1];
            [textField becomeFirstResponder];
        }

    }
}

But this code doesn't works. I think there is an issue with using fast enumeration?

Can somebody help with this? Thanks, Alex.


Some ideas to solving from c-had:

  1. Why are you setting up your textFieldsArray in textFieldShouldReturn:? Shouldn't this be done somewhere where initialization is done (e.g. viewDidLoad of the view controller)? The problem might be that your array is never actually set up, or is only partly set up. Also, putting the setup here means it will be called over and over, mutating your array each time and screwing things up.
  2. Your tags are off by one. You should be assigning them starting at 0, not 1 (or accounting for the difference in inputAccessoryViewDidSelectNext:). If your code was working, it would skip 2 text fields, as the first field would return tag 1, and you'd skip to objectAtIndex:2, which is the 3rd field.
  3. You don't account for going off the end. If you're at the last field, incrementing will not go back to the beginning.

I'd actually recommend not using tags at all. Instead, at setup time, just loop through the fields adding them in order to your array. Then, in inputAccessoryViewDidSelectNext:, use a counter instead of fast enumeration to figure out where you are.

Was it helpful?

Solution

  1. Why are you setting up your textFieldsArray in textFieldShouldReturn:? Shouldn't this be done somewhere where initialization is done (e.g. viewDidLoad of the view controller)? The problem might be that your array is never actually set up, or is only partly set up. Also, putting the setup here means it will be called over and over, mutating your array each time and screwing things up.
  2. Your tags are off by one. You should be assigning them starting at 0, not 1 (or accounting for the difference in inputAccessoryViewDidSelectNext:). If your code was working, it would skip 2 text fields, as the first field would return tag 1, and you'd skip to objectAtIndex:2, which is the 3rd field.
  3. You don't account for going off the end. If you're at the last field, incrementing will not go back to the beginning.

I'd actually recommend not using tags at all. Instead, at setup time, just loop through the fields adding them in order to your array. Then, in inputAccessoryViewDidSelectNext:, use a counter instead of fast enumeration to figure out where you are.

OTHER TIPS

I suggest do not use any array to have textFields, instead get the textFields from the cells of the tableView.

You can do in this way,

Set your textFields tag with the indexPath of cell in which it is.

Then use textFieldDidEnd delegate, from the tag get the cell of the next textfield later from cell get the textfield and make it first responder.

If you can get the cell from FDInputAccessoryView in below method, you can also do it here..

- (void) inputAccessoryViewDidSelectNext:(FDInputAccessoryView *)view`

According to the documentation, "[Fast] Enumeration is “safe”—the enumerator has a mutation guard so that if you attempt to modify the collection during enumeration, an exception is raised." That's probably why it doesn't work. You could probably accomplish the same thing with an ordinary while loop.

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