views:

31

answers:

1

Hi,

I have a table with 3 UITextFields added to the content views of cells (1 row per section). I have a 4th section that I insert so that I can scroll the particular field to be above the keyboard.

The problem I have (on the iPad only) is that when one of the text fields has firstResponder, it does not relinquish it when user taps on another field. Are there differences in the responder chain on an ipad? In the bellow code for my view controllers UITextFieldDelegate - textFieldShouldEndEditing does not get called when touching another field. Pressing the done button works as expected (unless another field has been touched).

Anything wrong with the code bellow?

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (!_editing++) {
        [self.tableView insertSections:[NSIndexSet indexSetWithIndex:4] withRowAnimation:UITableViewRowAnimationNone];
    }

    // scroll to section number in the text fields tag
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:textField.tag] atScrollPosition:UITableViewScrollPositionTop animated:YES];

    return YES;
}

- (void) textFieldDidBeginEditing:(UITextField *)textField {
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:textField action:@selector(resignFirstResponder)] autorelease]; 
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (_editing-- == 1) {
        // 
        [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:4] withRowAnimation:UITableViewRowAnimationNone];
    }

    self.navigationItem.rightBarButtonItem = nil;

    // do something with the text here...

    return YES;
}
A: 

OK, i have found a satisfactory workaround, it seems allowing the run loop to execute before the request to animate removing a section fixes the problem. I guess this is an apple bug? For anyone else worried about this issue - i was running iOS3.2.1 on the iPad.

I wont tick my answer, incase someone comes up with a proper reason!

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (_editing == 1) {
        [self performSelector:@selector(hideFinalSection) withObject:nil afterDelay:0.0f];
    }
    else {
        _editing--;
    }

    self.navigationItem.rightBarButtonItem = nil;

        // do something with the text here...

    return YES;
}

- (void) hideFinalSection {
    if (!(--_editing)) { 
        [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:4] withRowAnimation:UITableViewRowAnimationNone];
    }
}
Nick H247