patrick rogers
patrick rogers

Reputation: 21

clearing multiple text fields at once

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

Upvotes: 2

Views: 268

Answers (1)

coneybeare
coneybeare

Reputation: 33101

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];

Upvotes: 1

Related Questions