开发者

changing attributes on a collection of UITextFields

开发者 https://www.devze.com 2023-04-11 08:45 出处:网络
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开发者_JA

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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号