在iPhone中,我有一个带有UITextField的 View 。当我点击UITextField的清除按钮时,键盘将关闭,而不是清除UITextField中的文本。在iPad上,它可以正常工作。我该怎么做才能解决此问题?

最佳答案

只需清除字段resignFirstResponder(如果要隐藏键盘)并返回NO / false
注意:设置UITextField的“属性”检查器属性



因此在文本字段中进行编辑时,它将显示清除按钮。

// Objective-C

-(BOOL)textFieldShouldClear:(UITextField *)textField
{
    textField.text = @"";
    [textField resignFirstResponder];
    return NO;
}

// Swift
func textFieldShouldClear(textField: UITextField) -> Bool {
    textField.text = ""
    textField.resignFirstResponder()
    return false
}

07-28 02:00
查看更多