2011-08-05 171 views
1

这样做可能有一个非常简单的方法,但这是一个我已经有很长时间的编程技巧,但从来没有想过可能会有答案。关于对象名称的循环

考虑以下几点:

[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

好的,假设我不是一个完整的娃娃,甚至没有考虑这样做!我会使用像这样:[[buttonArray objectAtIndex:0] setTitle:[someArray objectAtIndex0]];对? – ThisDarkTao

+0

是的,但不是'0',使用迭代器('i'或其他)作为这两个数组的索引。 – MByD

+0

好吧。我仍然完全惊讶(并且有些尴尬),因为我没有早点把它弄碎。感谢让我醒来! :) – ThisDarkTao

2

有一种方法,使用自省,但你并不需要,只需创建按钮的排列和你遍历字符串数组以同样的方式重复它...