Bot
Bot

Reputation: 11855

Get array of all UITextFields

How can I get an array of all UITextFields in a view controller?

EDIT: I do not want to hardcode the textfields into an array. I actually want to get the list inside the delegate of all the fields from the caller of that delegate.

Upvotes: 7

Views: 8653

Answers (6)

Nirav Bhatt
Nirav Bhatt

Reputation: 6969

extension UIView 
{
   class func getAllSubviewsOfType<T: UIView>(view: UIView) -> [T] 
   {
       return view.subviews.flatMap { subView -> [T] in
       var result = UIView.getAllSubviewsOfType(view: subView) as [T]
       if let view = subView as? T {
           result.append(view)
       }
       return result
     }
   }

   func getAllSubviewsWithType<T: UIView>() -> [T] {
       return UIView.getAllSubviewsOfType(view: self.view) as [T]
   }
}

How to use with Text Fields:

let textFields = self.view.getAllSubviewsWithType() as [UITextField]

Upvotes: 1

Tim
Tim

Reputation: 14446

Recursive implementation to search all subviews' subviews: (this way you catch textfields embedded in uiscrollview, etc)

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

        if([x respondsToSelector:@selector(subviews)]){
            // if it has subviews, loop through those, too
            [textfieldarray addObjectsFromArray:[self findAllTextFieldsInView:x]];
        }
    }
    return textfieldarray;
}

-(void)myMethod{
   NSArray* allTextFields = [self findAllTextFieldsInView:[self view]];
   // ...
 }

Upvotes: 17

Himanshu Mahajan
Himanshu Mahajan

Reputation: 4799

-Use following code to get array that contains text values of all UITextField presented on View:

   NSMutableArray *addressArray=[[NSMutableArray alloc] init];

   for(id aSubView in [self.view subviews])
   {
           if([aSubView isKindOfClass:[UITextField class]])
           {
                  UITextField *textField=(UITextField*)aSubView;
                  [addressArray addObject:textField.text];
           }
   }
   NSLog(@"%@", addressArray);

Upvotes: 1

Cyprian
Cyprian

Reputation: 9453

EDIT Recursion without global variable (Just for Tim)

-(NSArray*)performUIElementSearchForClass:(Class)targetClass onView:(UIView*)root{

    NSMutableArray *searchCollection = [[[NSMutableArray alloc] init] autorelease];

    for(UIView *subview in root.subviews){

        if ([subView isKindOfClass:targetClass])  
            [searchCollection addObject:subview];

        if(subview.subviews.count > 0)
            [searchCollection addObjectsFromArray:[self performUIElementSearchForClass:targetClass onView:subview]];

    }

    return searchCollection;
}

Upvotes: -1

Paul.s
Paul.s

Reputation: 38728

If you know you need an NSArray containing all the UITextField's then why not add them to an array?

NSMutableArray *textFields = [[NSMutableArray alloc] init];

UITextField *textField = [[UITextField alloc] initWithFrame:myFrame];

[textFields addObject:textField]; // <- repeat for each UITextField

If you are using a nib then use an IBOutletCollection

@property (nonatomic, retain) IBOutletCollection(UITextField) NSArray *textFields;

Then connect all the UITextField's to that array

Upvotes: 3

Jeremy
Jeremy

Reputation: 9020

You can loop through the controller's view's subviews.

Here's a rough example:

NSMutableArray *arrOfTextFields = [NSMutableArray array];
for (id subView in self.view.subviews)
{
    if ([subView isKindOfClass:[UITextField class]])
       [arrOfTextFields addObject:subView]; 
}

Upvotes: -1

Related Questions