开发者

UITextfield clear button

开发者 https://www.devze.com 2023-02-22 15:35 出处:网络
How can I clear the field which currently has the cursor placed inside it? I have tried: if(textfield.isEditing)

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 pres开发者_StackOverflow中文版s the 'clear field button' all the fields clear Also instead of isEditing, I have tried to use tags on the UITextfields and do :

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

but this has the same effect.

How can I solve this problem?


try one or both of these...

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

Then add delegate...

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


Try out this in TextField's Delegate method didBeginEditing:

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

Hope this helps you.

0

精彩评论

暂无评论...
验证码 换一张
取 消