UITextFieldDelegate与UITextField控件事件 [英] UITextFieldDelegate vs UITextField control events

查看:552
本文介绍了UITextFieldDelegate与UITextField控件事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想处理对UITextField的更改,例如用户输入它;看起来这可以通过为该文本字段分配委托,然后让委托实现shouldChangeCharactersInRange,或者通过向textField添加目标,以及处理UIControlEventEditingChanged事件来完成。

If I want to handle changes to a UITextField, such as the user typing in it; it seems like this can be done either by assigning a delegate to that text field, and then having the delegate implement shouldChangeCharactersInRange, or by adding a target to the textField, and handling the UIControlEventEditingChanged event.

除了使用委托方法,您可以返回NO并因此阻止用户进行编辑,这两件事之间有什么区别吗?

Aside from the fact that with the delegate method, you can return NO and therefor stop the user from making the edit, is there any difference between these 2 things?

处理编辑开始或编辑结束的相同问题。可以使用适当的委托方法或适当的事件来完成。如果控制事件能够完成必要的工作,那么textField委托实际上是什么?

Same question for handling the beginning of editing or the ending of editing. It could be done either with the appropriate delegate methods or with the appropriate events. What is the textField delegate actually for if the control events can do the necessary work?

推荐答案

你是对的;你可以基本上通过两者做同样的事情,但UIControl是较低级别,并允许您通过 [UIControl addTarget:action:forControlEvents:] 将每个特定的UIEvent吸引到不同的目标因为只有一个代表。

You're right; you can essentially do the same thing via both, but UIControl is lower level and lets you siphon off each particular UIEvent to different targets via [UIControl addTarget:action:forControlEvents:] where as there is only a single delegate.

我也会说 UITextField委托协议只是作为管理行为的一种方式,作为UIControl / UIEvent的一种更方便,更高级别的替代方案。的UITextField。

I would also say that the UITextField delegate protocol is simply there as a more convenient, higher level alternative to UIControl/UIEvent as a way to manage the behaviour of a UITextField.

最常见的委托模式是UITableView DataSource和Delegate,我想说使用UITextField委托协议是非常相似的,因此看起来更直接,意图更明确直接从UIControl传递消息。

The most common delegate pattern is UITableView DataSource and Delegate and I would say that using the UITextField delegate protocol is quite similar and therefore looks far more straight forward with more defined intentions than handing the messages from UIControl directly.

这篇关于UITextFieldDelegate与UITextField控件事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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