ThisDarkTao
ThisDarkTao

Reputation: 1062

For-loop on object names

There is probably a very simple way of doing this, but this is a programming niggle I've had for a long time but never thought there might be an answer to.

Consider the following:

[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

I know I can use a For {...} loop to provide the array indexes, but is there any way of iterating through the button names programmatically, i.e someButton1, someButton2 etc?

Upvotes: 1

Views: 224

Answers (2)

Jordão
Jordão

Reputation: 56477

You can put the buttons themselves in an array and iterate over it.

Upvotes: 2

MByD
MByD

Reputation: 137322

There is a way, using introspection, but you don't need to, simply create an array of buttons and iterate it in the same way you iterate the array of strings...

Upvotes: 2

Related Questions