3

I am using number pad for my UITextField's.

I have set the delegate for my textfields.

I used the required delegate methods to handle the different events.

But I am unable to capture the event when user taps on x mark on number pad when a textfield hasBecomeFirstResponder and there are no characters entered in a textfield.

However, I can get the delete event when user pressed any character(or when there is atleast one character present in the textfield) and then press on delete key.

I set the keypad type as number pad in storyboard.

Delegate is set in both storyboard and also in code.

Here is the code if you want to have a look:

 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

          if ([string length]==0) {
                NSLog(@"deleted");
                return YES;
            }

}

I think the above method should get hit whenever the I press any key on keyboard. But in my case, this method is not hit when I press the delete key when the textfield is empty.

My question is: I am looking for a way to detect press on delete key in an efficient way. I don't want to add a button on top of the delete button. As one answer suggested to add a button to the delete key.

Is it possible to add the target method to the delete key,so that whenever I press delete key, that method would get hit? (Or) Is is possible to get a delete key event when there are no characters in a textfield and when the textfield has become the first responder?

I want to know because, I have four textfields placed in a horizontal stack view. When the user enters one character in first textfield, I am making the second text field as first responder,and the user enters the text on second textfield.

And the same way, I want to make the other textfield become first responder, when the user press delete key.Please note that, when I want to move back to other textfield by pressing the delete key, there will be no characters entered in the current textfield.And I am unable to detect delete key event when the textfield is empty.

enter image description here

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
  • ooh,, I tried by searching with "detect delete key when text filed is first responder"... I didnt try the empty keyword :P . I should really have shown some research effort!!! @rmaddy – Teja Nandamuri Dec 14 '15 at 16:23

1 Answers1

0

Show either a space or a digit.

meaning-matters
  • 21,929
  • 10
  • 82
  • 142
  • 1
    so you are saying to put a space when the textfield has become first responder ? – Teja Nandamuri Dec 14 '15 at 16:15
  • 2
    How does this answer question about whether you can detect the delete key on an empty text field? – rmaddy Dec 14 '15 at 16:15
  • @rmaddy It's a pragmatic solution, not a theoretical answer. What you point at as duplicate isn't very helpful. – meaning-matters Dec 14 '15 at 16:18
  • 1
    what if I want to enter a character ? Do I need to delete the space and replace it with new character ? If not, the entered character will be in a different position. I am showing only the first character to the user in the textfield. – Teja Nandamuri Dec 14 '15 at 16:18
  • @Mr.T Yes, you need just a little bit of logic to remove the space and replace it with the character typed, and vice versa when the user taps delete. – meaning-matters Dec 14 '15 at 16:21