对象名称的For循环

时间:2011-08-05 16:27:40

标签: objective-c for-loop

可能有一种非常简单的方法可以做到这一点,但这是我已经有很长一段时间的编程问题,但从未想过可能会有答案。

请考虑以下事项:

[someButton1 setTitle:[NSString stringWithFormat:@"%@",[someArray objectAtIndex:0]]];
[someButton2 setTitle:[NSString stringWithFormat:@"%@",[someArray objectAtIndex:1]]];
[someButton3 setTitle:[NSString stringWithFormat:@"%@",[someArray objectAtIndex:2]]];
[someButton4 setTitle:[NSString stringWithFormat:@"%@",[someArray objectAtIndex:3]]];
[someButton5 setTitle:[NSString stringWithFormat:@"%@",[someArray objectAtIndex:4]]];
[someButton6 setTitle:[NSString stringWithFormat:@"%@",[someArray objectAtIndex:5]]];
... etc

我知道我可以使用For {...}循环来提供数组索引,但有没有办法以编程方式迭代按钮名称,即someButton1,someButton2等?

2 个答案:

答案 0 :(得分:2)

有一种方法,使用内省,但你不需要,只需创建一个按钮数组并按照迭代字符串数组的方式迭代它...

答案 1 :(得分:2)

您可以将按钮本身放在一个数组中并迭代它。