如何使文本字段框删除用户第一次按键时的所有内容?
- (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;
}
更改
appears while editing
中的textfield属性清除按钮模式
或其他选择只需使用单行,您需要在其中添加
yourtextfieldname.text=@""; //it is used for clear the textfield values
斯威夫特
yourtextfieldname.text=""
或者其他方式
clearField =@"YES";
if([clearField isequaltostring:@"YES"]) //check this line in
{
tfieldDOB.text = @"";
clearField =@"NO";
}
实现文本字段的委托方法
textFieldShouldBeginEditing:
,并在文本字段即将编辑时将文本设置为空字符串。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[textField setText:@""];
return YES;
}
或者您可以将文本字段的属性
clearsOnBeginEditing
设置为
[textField setClearsOnBeginEditing:YES];
并且在编辑开始时它会清除文本