How to delete the contents of a UITextField programmatically?

前端 未结 2 814
[愿得一人]
[愿得一人] 2021-01-27 08:14

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 u

相关标签:
2条回答
  • 2021-01-27 08:29

    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;
    
    0 讨论(0)
  • 2021-01-27 08:33

    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.

    0 讨论(0)
提交回复
热议问题