是否可以为数字键盘上的删除键添加目标方法?

Is it possible to add target method for the delete key on the number pad?

我正在为 UITextField's 使用数字键盘。

我已经为我的文本字段设置了委托。

我使用了所需的 delegate 方法来处理不同的事件。

但是当用户点击数字键盘上的 x 标记时 textfield hasBecomeFirstResponder 并且有textfield.

中没有输入任何字符

但是,当用户按下任何字符(或者当文本字段中存在至少一个字符)然后按下删除键时,我可以获得删除事件。

我在storyboard中将键盘类型设置为数字键盘。

故事板和代码中都设置了委托。

如果你想看一下,这里是代码:

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

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

}

我认为只要我按下键盘上的任意键,上述方法就会被触发。但在我的例子中,当我在文本字段为空时按下删除键时,这个方法没有被命中。

我的问题是: 我正在寻找一种以有效方式检测按下删除键的方法。我不想在删除按钮之上添加一个按钮。正如一个答案所建议的那样,向删除键添加一个按钮。

是否可以将目标方法添加到删除键中,以便每当 我按删除键,那个方法会被击中吗? (要么) 当文本域中没有字符并且文本域已成为第一响应者时,是否有可能获得删除键事件?

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.

显示 space 或数字。