gpt4 book ai didi

ios - 按下 UITextField 的小十字按钮时的 Action

转载 作者:可可西里 更新时间:2023-11-01 06:21:15 25 4
gpt4 key购买 nike

我可以使用以下代码添加一个小十字按钮,用于在 UITextField 中单击一次清除所有文本。

textField.clearButtonMode = UITextFieldViewModeWhileEditing;

我还实现了 UITextFieldDelegate

我想根据我的 UITextField 中的更改激活/停用 UINavigationBar 的右栏按钮。规则很简单:如果文本字段至少有一个字符,则只启用该按钮,否则禁用它。

我实现了 UITextField 的委托(delegate)方法 textField:shouldChangeCharactersInRange: 以不断检查要求并更新按钮的状态。

现在我的问题是,当我单击文本字段上的小十字按钮时,方法 textField:shouldChangeCharactersInRange: 没有被调用。因此缺少所需的操作。如何识别清除按钮的点击事件?

一些图片来展示我在说什么。

初始阶段:这里,“完成”按钮被禁用,因为文本字段中没有字母

输入字母时,启用“完成”按钮

现在,如果我点击清除按钮,“完成”按钮仍然可用。这是错误的。

最佳答案

使用 delegate method textViewShouldClear: :

Asks the delegate if the text field’s current contents should be removed.

- (BOOL)textFieldShouldClear:(UITextField *)textField

Parameters

textField

The text field containing the text.

Return Value

YES if the text field’s contents should be cleared; otherwise, NO.

Discussion

The text field calls this method in response to the user pressing the built-in clear button. (This button is not shown by default but can be enabled by changing the value in the clearButtonMode property of the text field.) This method is also called when editing begins and the clearsOnBeginEditing property of the text field is set to YES.

Implementation of this method by the delegate is optional. If it is not present, the text is cleared as if this method had returned YES. Availability

* Available in iOS 2.0 and later.

声明于UITextField.h

关于ios - 按下 UITextField 的小十字按钮时的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7304311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com