UITextFieldDelegate 与 UITextField 控件事件

发布于 2024-10-16 06:27:15 字数 295 浏览 9 评论 0原文

如果我想处理 UITextField 的更改,例如用户在其中键入;看起来这可以通过将委托分配给该文本字段,然后让委托实现 shouldChangeCharactersInRange 来完成,或者通过将目标添加到文本字段并处理 UIControlEventEditingChanged 事件来完成。

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

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

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.

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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倾城花音 2024-10-23 06:27:15

shouldChangeCharactersInRange 在更改发生之前调用,让您有机会“取消”更改。 UIControlEventEditingChanged 在更改发生后调用。

您可以在 shouldChangeCharactersInRange 中确定 textField 的结果值,但您必须使用提供的范围手动将 replacementString 应用于现有文本。 (通过NSString stringByReplacingCharactersInRange)。如果您想知道生成的文本,使用 UIControlEventEditingChanged 会更简单、更高效。

shouldChangeCharactersInRange 通常用于实现输入的验证检查 - 也就是说,您可以在输入时过滤字符/粘贴的文本。例如,如果字段用于电话号码,则当用户键入非数字字符或尝试粘贴非数字文本时,您可以返回 FALSE

如果您坚持使用 UIControlEvent 方法,您可能会发现可以为多个控件重用代码。

shouldChangeCharactersInRange is called before a change occurs, and gives you opportunity to 'cancel' the change. UIControlEventEditingChanged is called after the change occurred.

You can determine the resulting value of the textField in shouldChangeCharactersInRange, but you have to manually apply the replacementString to the existing text, using the supplied range. (via NSString stringByReplacingCharactersInRange). If you want to know the resulting text, it's easier and more efficient to use UIControlEventEditingChanged.

shouldChangeCharactersInRange is often used to implement validation checking of input - that is, you can filter characters/pasted text as it is entered. If a field is for phone numbers, for example, you can return FALSE if the user types a non numeric character, or attempts to paste in text that isn't numeric.

You might find a case where you can reuse code for multiple controls if you can stick with the UIControlEvent-methods.

带刺的爱情 2024-10-23 06:27:15

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

我还想说 UITextField 委托协议< /a> 只是 UIControl/UIEvent 的更方便、更高级别的替代方案,作为管理 UITextField 行为的方法。

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

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.

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.

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.

じее 2024-10-23 06:27:15

我发现原始问题中提出的两种方法之间的一个关键区别是,委托“shouldChangeCharactersInRange”之前调用UITextField更改。 UIControlEventEditingChanged 的目标在 UITextField 中的值发生更改之后被调用。

如果您使用这些事件来确保(例如)在启用“完成”按钮之前对话框中的所有字段都已完全填写,则目标方法可能更适合您。它对我有用。

One key difference I've found between the two approaches posed in the original question is that the delegate "shouldChangeCharactersInRange" gets called BEFORE the value in the UITextField changes. The target for UIControlEventEditingChanged gets called AFTER the value in the UITextField changes.

In the case that you're using these events to make sure (for example) that all fields in a dialog are completely filled in before enabling a "Done" button, the target approach may work better for you. It did for me.

本王不退位尔等都是臣 2024-10-23 06:27:15

委托方法是同质化 UITextFieldUITextView 行为的方法。

UITextView 没有控件事件。相反,UITextFieldDelegateUITextviewDelegate 提供并行方法。

The delegation approach is the way to homogenize UITextField and UITextView behavior.

UITextView does not have control events. In contrast, UITextFieldDelegate and UITextviewDelegate provide parallel methods.

时光病人 2024-10-23 06:27:15

我发现 shouldChangeCharactersInRange 传递相同的 NSRange 来插入和删除文本。您附加一个空格,然后将其删除,并且来自 shouldChangeCharactersInRange 的参数与文本的重复部分没有区别。

因此,shouldChangeCharactersInRange 实际上无法预测结果文本。

I have found out that shouldChangeCharactersInRange passes the same NSRange for insertion and deletion of text. You append a space and then delete it, and the parameters from shouldChangeCharactersInRange are indistinguishable from duplication of the text.

So shouldChangeCharactersInRange actually cannot predict the resulting text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文