Reputation:
In my project I am adding above "6" textfields on my storyboard and they are "Email","name","Firstname"...etc textfields
now I want to set background color for all this textfields using "For" loop
is it possible to set using for loop?
Upvotes: 0
Views: 102
Reputation: 78
if all of your text fields are inside the same view controller, simply add inside the view controller's implementation:
for (UITextField *textField in self.view.subviews) {
if ([textField isKindOfClass:[UITextField class]]) {
textField.backgroundColor = [UIColor exampleColor];
}
}
if they are not in the same view controller, add all of the view controller into an array and do it from there:
NSArray *array = @[emailField, nameField, etc...]
for (UITextField *textField in array)
textField.backgroundColor = [UIColor exampleColor];
}
Upvotes: 0
Reputation: 2999
You can set background color in storyboard. as you created those in storyboard which is good practice. But if you created those textfield programatically then it would be good practice to set BG color programatically by a for loop.
Upvotes: 0
Reputation: 9012
You could do it purely in code like this:
NSArray *textFields = @[self.emailTextField, self.nameTextField, self.firstNameTextField ... etc];
for (UITextField *textField in textFields) {
textField.backgroundColor = [UIColor redColor];
}
Alternatively you can create an IBOutletCollection
in your storyboard and hook it up all text fields to a single property on your view controller:
@interface MyViewController : UIViewController
@property (nonatomic, strong) IBOutletCollection(UITextField) NSArray *textFields;
@end
Then iterate of self.textFields
in a similar manner to my first example.
Upvotes: 2