Create a loop in which you create 50 Buttons and store them in an mutable array.
Shuffle the array, best by crating a category on NSMutableArray and pick the first 6 objects from it.
You'll find a Category with several convenient methods in my arraytools
One important information is missing: How do the methods the buttons should trigger, look like?
If you have something regular like -pressedButton<No>:
, the for-loop could look like:
create and store 50 buttons
self.buttons = [NSMutableArray arrayWithCapacity:50]; //NSMutableArray
for (int i=0; i<50, i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(NSSelectorFromString([NSString stringWithFormat:@"pressedButton%i:", i]))
forControlEvents:UIControlEventTouchDown];
[buttons addObject:button];
}
shuffle and pick 6 buttons
[buttons shuffle]; // see arraytools
NSArray *sixButtons = [buttons objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,6)]];
and finally calculate a frame for each button in sixButtons
, and add it to the designated view [view addSubview:button];
If you don't have a method for every Button, you can distinguish the buttons by their indexes within the buttons array. But be careful: In this case you shouldn't shuffle it. Instead you should transform it int an unmutable array
NSMutableArray *buttonsTemp = [NSMutableArray arrayWithCapacity:50];
for (int i=0; i<50, i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[buttonsTemp addObject:button];
}
self.buttons = [NSArray arrayWithArray:buttonsTemp]; //Member of type NSArray
Now you can pick 6 buttons randomly
NSSet *sixButtos = [buttons setWithRandomElementsSize:6];//see arraytools
-buttonPressed:
could be like this:
-(void) buttonPressed:(UIButton *)sender
{
NSUInteger buttonIndex = [buttons indexOfObject:sender];
//Now you can use if or switch to distinguish, what needs to be done
}