ios 如何擦除或清除 UITextField
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23151808/
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
how to erase or clear UITextField
提问by user3331589
how to clear text field in ios, How can I make a textfield box remove all content on the users first keypress?
如何清除ios中的文本字段,如何使文本字段框删除用户第一次按键时的所有内容?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([tfieldDOB.text length] == 4)
{
tfieldDOB.text=[NSString stringWithFormat:@"%@/",tfieldDOB.text];
}
else if([tfieldDOB.text length]==7)
{
tfieldDOB.text=[NSString stringWithFormat:@"%@/",tfieldDOB.text];
}
return YES;
}
回答by Anbu.Karthik
change the textfield attribute clear button mode in appears while editing
更改文本字段属性清除按钮模式 appears while editing
or other choice just use the single line, where you need to add
或其他选择只需使用单行,您需要在其中添加
yourtextfieldname.text=@""; //it is used for clear the textfield values
Swift
迅速
yourtextfieldname.text=""
or another way
或其他方式
clearField =@"YES";
if([clearField isequaltostring:@"YES"]) //check this line in
{
tfieldDOB.text = @"";
clearField =@"NO";
}
回答by Zen
Implement the text field's delegate method textFieldShouldBeginEditing:
and set the text as empty string when the text field is just about to being editing.
实现文本域的委托方法,textFieldShouldBeginEditing:
并在文本域即将被编辑时将文本设置为空字符串。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[textField setText:@""];
return YES;
}
Or you can set the property clearsOnBeginEditing
of the textfield as
或者您可以将clearsOnBeginEditing
文本字段的属性设置为
[textField setClearsOnBeginEditing:YES];
and it will clear the text when editing begins
它会在编辑开始时清除文本