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 开发者_StackOverflowis the variable I am sending.
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;
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.
精彩评论