2

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?

icecreamhead
  • 111
  • 1
  • 12

3 Answers3

1

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];
    }
}
Graham Perks
  • 23,007
  • 8
  • 61
  • 83
0

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.

Eran Boudjnah
  • 1,228
  • 20
  • 22
0

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...

Nate Symer
  • 2,185
  • 1
  • 20
  • 27
  • The textField's delegate is already set to self but it doesn't make a difference. – icecreamhead Mar 01 '12 at 00:02
  • Find anything that changes the UITextfield delegate (except the one that sets it to self) and comment it out. Then see if it works. I would use something that writes to a file because NSLog is hard in your situation. – Nate Symer Apr 17 '12 at 11:48