How to delete the contents of a UITextField programmatically?
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开发者_如何学Go me no matter what I try.
You can just assign tags to each textfield in a sequence using tag property in interface builder and then get the textField like this
UITextField *txtf = (UITextField*)[self.view viewWithTag:aTagValue];
txtf.text = @""; or nil;
There are several ways of tackling this.
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.
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 one IBOutletCollection
and loop over it. This is probably the simplest, most straightforward way of doing it.
精彩评论