xcode UITextfield 清除按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5584169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:45:43  来源:igfitidea点击:

UITextfield clear button

iphoneobjective-cxcode

提问by user648244

How can I clear the field which currently has the cursor placed inside it?

如何清除当前放置光标的字段?

I have tried:

我试过了:

if(textfield.isEditing)
   textfield.text = @""; 

This works for me, but if I select all 3 fields, and press the 'clear field button' all the fields clear Also instead of isEditing, I have tried to use tags on the UITextfields and do :

这对我有用,但是如果我选择所有 3 个字段,然后按“清除字段按钮”清除所有字段,而不是 isEditing,我尝试在 UITextfields 上使用标签并执行以下操作:

if(textfield.tag == 1)
    textfield.text = @"";

but this has the same effect.

但这具有相同的效果。

How can I solve this problem?

我怎么解决这个问题?

回答by Jordan

try one or both of these...

尝试其中一种或两种...

textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.clearsOnBeginEditing = YES;

Then add delegate...

然后添加委托...

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    return YES;
}

回答by Terry Wilcox

Try out this in TextField's Delegate method didBeginEditing:

在 TextField 的 Delegate 方法中试试这个 didBeginEditing:

if ([textfield isFirstResponder]) {
  textfield.text = @"";
}

Hope this helps you.

希望这对你有帮助。