当按下UITextField的小十字按钮时执行操作 [英] Action when the small cross button of UITextField is pressed

查看:104
本文介绍了当按下UITextField的小十字按钮时执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  textField.clearButtonMode = UITextFieldViewModeWhileEditing; 

我还实现了 UITextFieldDelegate



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



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



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



一些照片显示我在说什么。



初始阶段:此处的完成按钮已停用,因为文本字段中没有字母



键入字母时,将启用完成按钮



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

解决方案

使用委托方法 textViewShouldClear:


委托如果文本字段的当前内容应该
删除。

   - (BOOL)textFieldShouldClear:(UITextField *)textField 

参数

  textField 



返回值


b

YES 如果文本字段的内容应该被清除;否则 NO



讨论



字段调用此方法以响应用户按下
内置清除按钮。 (默认情况下不显示此按钮,但可以通过更改
文本字段的 clearButtonMode 属性中的值启用
。)此方法也在编辑开始时调用,并且文本字段的
clearsOnBeginEditing 属性设置为 YES p>

由委托实现此方法是可选的。如果
不存在,则文本将被清除,如同此方法返回 YES
可用性

  *适用于iOS 2.0及更高版本。 


已声明在
UITextField.h


I can add a small cross button that is used to clear all the text in a single click in a UITextField with the following code.

textField.clearButtonMode = UITextFieldViewModeWhileEditing;

I also implemented UITextFieldDelegate.

I want to activate/deactivate the right bar button of the UINavigationBar according to the changes in my UITextField. The rule is simple: if the text field has at least one character, just enable the button, else disable it.

I implemented the UITextField's delegate method textField:shouldChangeCharactersInRange: to constantly check the requirement and update the status of the button.

Now my problem is, when I click the small cross button on the text field, the method textField:shouldChangeCharactersInRange: is not called. So the required action is missing. How can I identify the hit event of the clear button?

Some pictures to show what I'm talking about.

Initial stage: here, the "Done" button is disabled because there are no letters in the text field

When typing a letter, the "Done" button is enabled

Now, if I click on the clear button, the "Done" button is still enabled. This is wrong.

解决方案

Use the 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.

Declared In UITextField.h

这篇关于当按下UITextField的小十字按钮时执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆