如何以编程方式删除UITextField的内容? [英] How to delete the contents of a UITextField programmatically?

查看:54
本文介绍了如何以编程方式删除UITextField的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个iPhone应用程序,该应用程序具有许多UITextField,这些UITextField要求用户输入一堆数据.我想拥有一个简单的删除"功能,该功能允许用户丢弃他们输入到多个字段中的所有数据.

I am writing an iPhone app that has a number of UITextFields that requires the user to input a bunch of data. I would like to have a simple 'delete' facility that allows the user to discard any data they have put in to the multiple fields.

几天来我一直在努力解决这个问题.我似乎找不到找到进入各个文本字段并更改文本的方法.无论我尝试什么,我的应用都会向我抛出错误.

I've been struggling with this issue for a few days now. I can't seem to find a way of reaching into the individual text fields and changing the text. My app throws errors at me no matter what I try.

推荐答案

有几种解决方法.

您可以编写代码来分别擦除每个代码.为此,您为每个对象创建一个 IBOutlet ,并将空白字符串分配给它们的 text 属性.如果只有几个字段,那是最简单的.如果您还有更多,那就不好了.

You can write code to wipe each of them individually. To do this, you create an IBOutlet for each one, and assign a blank string to their text properties. If you only have a couple of fields, that's simplest. If you have more, it's not so good.

您可以使它们成为公共容器视图的所有子级,并在其子视图上循环.这样,您只需要连接一个 IBOutlet .

You can make them all children of a common container view and loop over its subviews. That way, you only need to hook up a single IBOutlet.

您可以递归遍历整个视图层次结构,并清空所有找到的文本字段.

You can recurse over your entire view hierarchy and blank out all the text fields you find.

Apple并没有很好地记录另一种方法,是使用 IBOutletCollection .这样,您可以将所有文本字段分配给一个 IBOutletCollection 并在其上循环.这可能是最简单,最直接的方法.

Another approach, which isn't very well documented by Apple, is to use an IBOutletCollection. That way, you can assign all of your text fields to the one IBOutletCollection and loop over it. This is probably the simplest, most straightforward way of doing it.

这篇关于如何以编程方式删除UITextField的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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