开发者

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

开发者 https://www.devze.com 2023-03-07 12:36 出处:网络
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:

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.

0

精彩评论

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