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.
There are several ways of tackling this.
You can write code to wipe each of them individually. To do this, you create an
IBOutletfor each one, and assign a blank string to theirtextproperties. If you only have a couple of fields, that’s simplest. If you have more, it’s not so good.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.
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 oneIBOutletCollectionand loop over it. This is probably the simplest, most straightforward way of doing it.