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.
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]];
// ...
}
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
-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);
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With