Mc.Stever
Mc.Stever

Reputation: 792

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? TIA.

Upvotes: 1

Views: 152

Answers (3)

EmptyStack
EmptyStack

Reputation: 51374

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!

Upvotes: 0

aroth
aroth

Reputation: 54806

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];

Upvotes: 0

Narayana Rao Routhu
Narayana Rao Routhu

Reputation: 6323

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;
         }
    }

Upvotes: 1

Related Questions