views:

84

answers:

2

I am trying to figure out the code for clearing multiple text fields at once. I know that there is another question like this with answers but could I get a little more information or code examples. I have 16 text fields in my app.

Thanks

+1  A: 

Check out the UITextFieldDelegate methods, specifically, textFieldShouldClear. This is called when someone taps the little x in the text field. I am going to give you a way to clear all textfields when one of those is tapped or any other button is tapped

- (void)clearAllTextFields {
    for (UITextField *textField in [self textFields]) { //textFields is an rray that is holding pointers to your 16 text fields
        [textField setText:nil];
    }
}

If you want this to happen on a button press or something, add it as a target. Here is how you would do it if one of the x's in the fields are tapped:

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    [self clearAllTextFields];
    return YES;
}

UPDATE:

UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(0,0,100,44)] autorelease];
[button setTitle:@"blah forState:UIControlStateNormal];
[button addTarget:self action:@selector(clearAllTextFields) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
coneybeare
Hi,I added the code but have errors,am I missing something? Should I add a IBOutlet for the button and synthesize,also "blah" should be the title of the button? Thanks for your help with thispatrick
patrick rogers
No you dont need IBOutlet unless you are using NIBs. Blah is the title. I suggest reading the Apple iPhone Developers Guide as you start your way down the path of iPhone development.
coneybeare
A: 

thanks for the help My text fields are in a grid and I want to add a button underneath to "start over" I need a little more help in connecting that button to clear the text fields. thanks again

Patrick rogers
This is not a place to add another question. please delete this answer and modify your original question
coneybeare