2011-08-05 37 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

有一種方法,使用自省,但你並不需要,只需創建按鈕的排列和你遍歷字符串數組以同樣的方式重複它...