如何擦除或清除UITextField

use*_*589 7 iphone objective-c uitextfield ios swift

如何在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;

}
Run Code Online (Sandbox Code Playgroud)

Anb*_*hik 12

更改文本字段属性清除按钮模式 appears while editing

或其他选择只需使用您需要添加的单行

yourtextfieldname.text=@"";  //it is used for clear the textfield values 
Run Code Online (Sandbox Code Playgroud)

迅速

yourtextfieldname.text=""
Run Code Online (Sandbox Code Playgroud)

或另一种方式

 clearField =@"YES";

if([clearField isequaltostring:@"YES"])  //check this line in 
{
    tfieldDOB.text = @"";
    clearField =@"NO";
}
Run Code Online (Sandbox Code Playgroud)


Zen*_*Zen 5

实现文本字段的委托方法,textFieldShouldBeginEditing:并在文本字段即将编辑时将文本设置为空字符串.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    [textField setText:@""];
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

或者您可以将clearsOnBeginEditing文本字段的属性设置为

[textField setClearsOnBeginEditing:YES];
Run Code Online (Sandbox Code Playgroud)

并在编辑开始时清除文本