changing attributes on a collection of UITextFields
I have a collection of UITextFields in a view. I need to disable then all and later enable them. Currently, I change each individually. Is there a way to do this programatically in a loop? TI开发者_JAVA百科A.
Use this it will help you enabled=NO or YES
for(id viewid in [self.view subviews])
{
if([viewid isKindOfClass:[UITextField class]])
{
UITextField *txt_temp = (UITextField *)viewid;
txt_temp.enabled=NO;
}
}
Assuming your UITextField
instances are held within a collection called myFieldCollection
, you could do something like:
- (void) disableFields {
for (UITextField* field in myFieldCollection) {
field.enabled = NO;
}
}
- (void) enableFields {
for (UITextField* field in myFieldCollection) {
field.enabled = YES;
}
}
I'm assuming based upon your opening statement that you already have them in a collection. If you do not, you can easily use Interface Builder to set up a "Referencing Outlet Collection" for the text fields.
To use the methods above, you would simply do:
//disable
[self disableFields];
//enable
[self enableFields];
NSArray *array = [view subviews];
Disable the subviews:
[array makeObjectsPerformSelector:@selector(setEnabled:) withObject:(id)NO];
Enable the subviews:
[array makeObjectsPerformSelector:@selector(setEnabled:) withObject:(id)YES];
Note the withObject: parameter here. Just cast the boolean constants YES or NO to id as you cast the object types!
精彩评论