RNA_Parts
RNA_Parts

Reputation: 83

want to use a for loop to set property on multiple objects

Here is a rookie question. I want to use a for loop to set the visibility on several buttons. Here is what i wanted to do:

for (int y = 0; y < numberOfPeople; y++) {
    button[y].hidden = true;
} 

where I have 5 buttons named: button1, button2, button3, button4, button5 and number of people is the variable I am sending.

Upvotes: 2

Views: 175

Answers (2)

JeremyP
JeremyP

Reputation: 86651

NSArray* buttons = [NSArray arrayWithObjects: button1, button2, button3, button4, button5, nil];
for (id aButton in buttons)
{
    aButton.hidden = YES;
}

Has the advantage that the loop does not need to know how many buttons it has got.

Upvotes: 1

Dietrich Epp
Dietrich Epp

Reputation: 213368

You can do this using an array:

NSButton *buttons[5];

You can either define your instance variable this way, or you can do this:

NSButton *buttons[5] = { button1, button2, button3, button4, button5 };
for (int i = 0; i < 5; ++i)
    button[i].hidden = YES;

Upvotes: 4

Related Questions