Zach Smith
Zach Smith

Reputation: 5684

iPhone SDK Clearing Multiple Text Fields at Once

If I have multiple text fields and would like to have a button that clears all text fields at once, what tutorials or direction should I look into doing this? Any help would be greatly appreciated.

Upvotes: 0

Views: 463

Answers (2)

TechZen
TechZen

Reputation: 64428

If you need blanket clear textfields often, it might be worth your time to create a subclass of UITextField with a clearAllText method. Then you can just broadcast the message to all the subviews in a view and the ones with the method will clear their text and the one's without will ignore it. (More neatly, you can check that individual subviews respond to the message before sending it.) That way, you don't have to track which objects are textfields and which are not. Very useful if you have a dynamic layout wherein the number of textfields changes.

Upvotes: 2

Seva Alekseyev
Seva Alekseyev

Reputation: 61378

You start by putting a handler on that button (Touch Up Inside event). Then, you have several approaches.

You can have an outlet for each text box, in the handler, you set the text property to nil for each.

You can scroll through the subviews collection of your view, and for each textbox you encounter, set set the text property to nil. This leaves no textbox on the view untouched. You will have to recurse if the view hierarchy is nested.

You can assign numeric tags in a certain range to each textbox, then you go through the range, retrieve the view by tag and set text to nil. This is how it's normally done in Win32 API, in case you care.

Upvotes: 1

Related Questions