0

Need an Assistance.My question is, how do i ensure that all textfields in a View are empty or not ..

I know that it can be achieved by a for loop followed by all textfields length are greater than zero.

Is there any method available to check this ?? TY in Advance.

Vicky_Vignesh
  • 584
  • 2
  • 14

2 Answers2

1

You can loop through all subviews of your view, and pick all of the UITextField instances.

//replace self.view to whatever your own view

- (void)checkAllTextField {
    for (UIView *view in self.view.subviews) {
        if ([view isKindOfClass:[UITextField class]]) {
            UITextField *textField = (UITextField *)view;
            NSLog(@"This is a UITextField, length: %lu", textField.text.length);
        } else {
            NSLog(@"not a UITextField");
        }
    }
}
ronan
  • 1,611
  • 13
  • 20
0

You can get array of all text fields using recursive function and then check for Blank in textfield.

-(NSArray*)findAllTextFieldsInView:(UIView*)view{
    NSMutableArray* textfieldarray = [[[NSMutableArray alloc] init] autorelease];
    for(UIView x in [view subviews]){
        if([x isKindOfClass:[UITextField class]])
            [textfieldarray addObject:x];

       else if([x respondsToSelector:@selector(subviews)] && [x subviews].count > 0){
            // if it has subviews, loop through those, too
            [textfieldarray addObjectsFromArray:[self findAllTextFieldsInView:x]];
        }
    }
    return textfieldarray;
}
PlusInfosys
  • 3,416
  • 1
  • 19
  • 33
  • 1
    Why is `x` declared as `id` instead of `UIView`? Why check if `x` responds to `subview` when all views respond to that selector? Why bother looking at the subviews of any text fields? – rmaddy Dec 19 '16 at 07:18