Clear TextFields with Button Tap?
Ok so i have looked and looked for a solution to this but havnt f开发者_StackOverflowound one that will help with what im trying to do. I have about 15 txtfields on one of my view's. i have found question and answers talking about the "x" that you can have come up beside a textfield so that when a user presses it.. it will clear the textfield. I already have the textfields set up via IB so that when the user clicks back into the textfield it will clear the contents. But, what i am trying to do is this. I have a "clear" button at the bottom, i want the user to be able to tap the clear button and all textfields clear whatever is in them. i have tried the following, but cant think of any other way for this to work.
- (IBAction)clear:(id)sender {
textfield.text = @"";
}
Does anyone else have a better idea?
@madmik3 , thanks for your response but i couldnt get that to work for some reason. i did however find a another question similar to mine, i guess i missed it in my earlier searches but it had an answer that worked. here is what i used...
(IBAction)clear:(id)sender {
for(int i=1; i<=2; i++)
{
UITextField *tf=(UITextField *)[self.view viewWithTag:i];
[tf setText:@""];
}
}
on the View's that had more than 2 textFields i replaces the "i<=2;" with "i<=9;" on one page and one i had to put 13 in there. But also i had to make sure i had each textField tag'd . But once again thanks for your help.
-(IBAction) clearAll:(id)sender
{
for (int i=0 ; i<[self.view.subviews length]; i++) {
UIView * v = [self.view.subviews objectAtIndex:i];
if ([v isKindOfClass:[UITextView class])
{
UITextView * tv = (UITextView*) v;
tv.text = @"";
}
}
}
精彩评论