0

I'm trying to create an iPhone application in which I have 50 buttons, numbered 1 to 50.

Now, I want to select any 6 out of 50 random buttons and it's value(numbers).

My questions are,

  1. How can I only select 6 buttons?
  2. Right now I'm thinking of taking 50 different IBOutlets and 50 IBActions respectively for each button to achieve my purpose. Is there any other better alternative that I can go for like an Array of Button?
  3. How do I retrieve the value of particular button?
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
MayurCM
  • 701
  • 1
  • 14
  • 31
  • 2
    It isn't quite clear what you mean. How many buttons will be displayed at once? What is the difference between the buttons? What happens when they are tapped? – jrturton Nov 15 '11 at 12:14
  • yes you can create NSArray or NSMutableArray to store your 50 buttons.. – Maulik Nov 15 '11 at 12:16
  • @jrturton, sorry for the trouble but here are the answers for your questions. Basically there ll be 1 to 50 numbers as a buttons on a screen from which user can select 6 numbers only. all the buttons are named as a number 1 to 50. when user taps on six random buttons each buttons value should be stored in one single string. – MayurCM Nov 17 '11 at 09:31

2 Answers2

3

You could create the buttons programmatically like this ( add them as subviews to a container view): How do I create a basic UIButton programmatically?

You can keep the number of the button in its tag propery.

To select a button with a given tag use [containerView viewWithTag:(NSInteger)].

To select 6 different random views you need to generate 6 different random numbers and use the above method.

You could do it like this.

bool used[51];
for (int i = 1 ; i <= 50 ; ++i)
    used[i] = false;
int count = 0;
int resulttags[6];
while (count < 6) {
    int index = 1 + random() % 50;
    if (!used[index]) {
         used[index] = true;
         resulttags[count++] = index;
    }
}
Community
  • 1
  • 1
silviupop
  • 638
  • 7
  • 14
0

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

}
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178