Why is textFieldDidEndEditing: not being called?

Chris Cleeland picture Chris Cleeland · Aug 4, 2009 · Viewed 31.2k times · Source

I've got a UITextField, and when text gets entered into that, I want to obtain the doubleValue from the text field and perform some computations and display them in a UITableView.

The delegate for the UITextField adopts the UITextFieldDelegate protocol, and implements both textFieldShouldReturn: and textFieldDidEndEditing: methods. textFieldShouldReturn: resigns first responder status which, according to docs should also trigger textFieldDidEndEditing:, but I never see textFieldDidEndEditing: called.

- (BOOL)textFieldShouldReturn:(UITextField*)theTextField {
    if (theTextField == thresholdValue) {
        [thresholdValue resignFirstResponder];
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    [self updateThresholdValue:textField];
}

It may be worth noting that I also tried connecting some of the textfield events to the delegate and having the event call updateThresholdValue: directly, but that didn't work either.

Answer

Miha Hribar picture Miha Hribar · Dec 9, 2010

Just ran into the same problem you described. After trying everything I could think of I added this delegate method:

// if we encounter a newline character return
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{   
    // enter closes the keyboard
    if ([string isEqualToString:@"\n"])
    {
        [textField resignFirstResponder];
        return NO;
    }
    return YES;
}

Now the textFieldShouldEndEditing fires and the text field resigns first responder.

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}