清除文本字段后的回调
Callback after text field has been cleared
我想执行代码在点击 UITextField 的清除按钮之后并且在文本字段被清除之后.
textFieldShouldReturn:在清除文本字段之前被调用。
textField:shouldChangeCharactersInRange:replacementString:点击清除按钮时根本不调用
最终目标是我在点击字符串时维护正在点击的字符串的一些图形表示。我使用 textField:shouldChangeCharactersInRange:replacementString: 为此,它运行良好,除非点击清除按钮。
(应该擦除图形表示)。
这里有一个想法:您可以将系统清除按钮替换为您自己的按钮。您需要自己处理清除文本字段(非常简单),然后您可以执行自定义动画等等。
创建一个按钮并将其设置为您的 UITextField
:
的 rightView
UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeSystem];
clearButton.frame = CGRectMake(0, 0, 45, 45);
clearButton.tintColor = myTextField.tintColor;
[clearButton setImage:[UIImage imageNamed:@"MY_CLEAR_IMAGE.png"] forState:UIControlStateNormal];
[clearButton addTarget:self action:@selector(onTextfieldClearButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
myTextField.rightView = clearButton;
myTextField.rightViewMode = UITextFieldViewModeWhileEditing;
处理动作:
-(void)onTextfieldClearButtonTouchUpInside:(UIButton*)clearButton
{
if([clearButton.superview isKindOfClass:[UITextField class]])
{
((UITextField*)(clearButton.superview)).text = @"";
//TODO: YOUR MAGIC GOES HERE
}
}
我想执行代码在点击 UITextField 的清除按钮之后并且在文本字段被清除之后.
textFieldShouldReturn:在清除文本字段之前被调用。
textField:shouldChangeCharactersInRange:replacementString:点击清除按钮时根本不调用
最终目标是我在点击字符串时维护正在点击的字符串的一些图形表示。我使用 textField:shouldChangeCharactersInRange:replacementString: 为此,它运行良好,除非点击清除按钮。 (应该擦除图形表示)。
这里有一个想法:您可以将系统清除按钮替换为您自己的按钮。您需要自己处理清除文本字段(非常简单),然后您可以执行自定义动画等等。
创建一个按钮并将其设置为您的 UITextField
:
rightView
UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeSystem];
clearButton.frame = CGRectMake(0, 0, 45, 45);
clearButton.tintColor = myTextField.tintColor;
[clearButton setImage:[UIImage imageNamed:@"MY_CLEAR_IMAGE.png"] forState:UIControlStateNormal];
[clearButton addTarget:self action:@selector(onTextfieldClearButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
myTextField.rightView = clearButton;
myTextField.rightViewMode = UITextFieldViewModeWhileEditing;
处理动作:
-(void)onTextfieldClearButtonTouchUpInside:(UIButton*)clearButton
{
if([clearButton.superview isKindOfClass:[UITextField class]])
{
((UITextField*)(clearButton.superview)).text = @"";
//TODO: YOUR MAGIC GOES HERE
}
}