Tap on UITextField's clear button hides keyboard instead of clearing text

前端 未结 5 1475
[愿得一人]
[愿得一人] 2021-02-07 03:54

In iPhone, I have a view which has a UITextField. When I tap on the clear button of UITextField\'s the keyboard dismissed instead of clearing the text

5条回答
  •  春和景丽
    2021-02-07 04:52

    First, check all the code blocks that related to your UITextField (especially the code yourTextField.hidden = YES;)

    Put break points and analyze every UITextField delegates that you implemented.

    (textFieldDidEndEditing,textFieldShouldEndEditing,textFieldShouldReturn.etc.)

    OR

    Implement the textFieldShouldClear delegate and write the code here to visible and clear your UITextField

    To do this, you have to set the clearButtonMode as below,

    yourTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    yourTextField.delegate = self;
    
    //For active keyboard again
    [yourTextField becomeFirstResponder];
    

    Then implement the textFieldShouldClear delegate

    YourClass.h

        @interface className : UIViewController 
    

    YourClass.m

    -(BOOL)textFieldShouldClear:(UITextField *)textField {
        yourTextField.hidden = NO;
        yourTextField.text = @"";
        return YES;
    }
    

提交回复
热议问题