Question

I'm making a weeapp for notification center using iOSOpenDev. I have a UITextField on a UIView and have implemented the UITextFieldDelegate protocol.

My problem is that the textFieldShouldClear method never gets called on clicking the clear button in the UITextField. Other interface methods such as shouldChangeCharactersInRange and textFieldShouldReturn are called without issue.

Any ideas why the interface method never gets called?

Was it helpful?

Solution

Make sure that the textfield's delegate is self:

theTextField.delegate = self;

I have heard that UIActionSheet sucks up the UITextFieldDelegate protocol, and the notification center might do the same...

OTHER TIPS

I had this problem when I was dismissing the keyboard when the user tapped elsewhere on the screen. I had a gesture recognizer looking for taps, and when a tap was detected, it would call resignFirstResponder on the text field. Unfortunately, that breaks the clear button.

What I did was filter the taps to be sure they are outside the table view, slightly complicated by having to manually trigger button taps:

// In: - (void)handleTap:(UITapGestureRecognizer *)sender {
// The user tapped outside a text field, drop the keyboard.
// Unfortunately this normally breaks the clear button, so we'll check that the
// tap is outside the table view (and therefore not on a clear button).
BOOL inButton = CGRectContainsPoint(self.signInButton.bounds, [sender locationInView:self.signInButton]);
BOOL inTable = CGRectContainsPoint(self.tableView.bounds, [sender locationInView:self.tableView]);
if (!inTable && !inButton ) {

    BOOL didEndEditing = [self.view endEditing:NO];

    // But... if we were editing a field (& therefore the keyboard is showing),
    // and if they tapped the sign in button, sign in. Not sure where the
    // onSignIn event is getting lost. The button does highlight. But the
    // call to endEditing seems to eat it.
    if (didEndEditing && inButton) {
        [self onSignIn:self.signInButton];
    }
}

Following Graham Perks's answer, I changed my resignFirstResponder call to:

[self.focusInput performSelector: @selector(resignFirstResponder)
                      withObject: nil
                      afterDelay: 0.2f];

Now the keyboard is automatically hidden on taps as intended, but the clear button functionality is back.

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