0xDE4E15B
0xDE4E15B

Reputation: 1294

Array of Buttons

for example, I need to create an array of buttons, which names are button1,button2...button10. could you show me, how to add those 10 buttons to an array?

for (int i = 0 ; i<=9; i++) {
[myarray addObject:???];

}

Thanks!

Upvotes: 1

Views: 1288

Answers (2)

user498982
user498982

Reputation:

NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];

int i = 0;
for (i=0;i<=9;i++) {
[myDictionary setObject:object forKey:[NSString stringWithFormat:@"object%i",i]];
}

That will give you a dictionary with object0 - object9. The setObject parameter can be any object (NSString, NSNumber, NSArray).

Oh, and dont forget to

[myDictionary release];

When you are done with it. And to get things from the dictionary you would do this (if the object stored was an NSString):

NSString *myString = [myDictionary objectForKey@"object0"];

Upvotes: 1

Scott Harwell
Scott Harwell

Reputation: 7465

I don't believe you can simply access an object in objective c by its name. You'll probably have to create IBOutlets to each of the buttons and then add the pointers to an NSDictionary on init. A dictionary would act very similar to an array, so it's probably in your best interest to skip an array and just use NSDictionary so you can label the buttons with the "key".

Upvotes: 0

Related Questions