Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a for loop to add 39 buttons to an array

I have 39 different UIButton variables in my .h file, but I would like to add each of them to an array without having to type out the same thing 39 times.

Is there a way that I could do this in a for loop?

The buttons are named accordingly: btn1,btn2,btn3 etc.

like image 279
max_ Avatar asked Apr 13 '11 22:04

max_


5 Answers

You might want to forego the 39 buttons in your header file and instead have a single array. I suspect that you want to use manual references so you can take advantage of Interface Builder, to control events and layout. I suggest doing something a little different.

Create a single property - an NSMutableArray. Then, when the view loads, create the buttons on the fly.

To access a button, use something like [self.arrayOfButtons objectAtIndex:38];. (In the case of 39 buttons, that would return the last button.);`

To create a button, you use the following:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

Note that you pass in the frame of your button's init method. The frame of your button is going to start in the top left corner of its container and your button will be 100 pixels square. The frame is an instance of CGRect. (You create a CGRect by calling the function CGRectMake(x,y,width,height).

To make 39 buttons, you might want to loop as follows, given an array to hold the buttons, myButtons and predefined numberOfButtons and dimension variables:

for(int i=0;i<numberOfButtons;i++){
  //Create the button
  UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x,y,width, height)];
  //Store the button in our array
  [self.myArray addObject:button];
  //Release the button (our array retains it for us)
  [button release];
}

Of course, you are going need to set unique values for x,y,width and height for each button or they will all overlap. Once you've created your buttons, you can do things with them, like set the label, or show them onscreen. To show them onscreen, you can do something like this:

for(UIButton *button in self.myButtons){
  //add the button to the view
  [self.view addSubview:button];
}

Of course, just adding buttons to the screen is useless. You need to be able to make them do something. To add an action to a button, you can use:

[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDown];

The first part, addTarget:self, says that this view controller handles the event that you're going to ask it to handle. action:@selector(someMethod:) tells the class what method to perform when the event occurs. Then, forControlEvents:UIControlEventTouchDown says that the said class should perform the said method when the button is tapped.

So, in your header:

#import <UIKit/UIKit.h>

@interface MyClass :UIViewController{

   NSMutableArray *myButtons; 
}

@property (nonatomic, retain) NSMutableArray *myButtons;

//Use UIButton as the sender type if you want 
- (void)someMethod:(id)sender;

// ... Other code can go here of course
@end

And in your implementation, you can use this:

#import MyClass.h


@implementation MyClass

- (id)init{

  self = [super init];

  if(self){

     NSMutableArray *temp = [[NSMutableArray alloc] init];
     self.myButtons = temp;
     [temp release];

  }

  return self;

}


- (void)viewDidLoad{

  //
  // Create the buttons
  //

  for(int i=0;i<numberOfButtons;i++){
    //Create the button
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x,y,width, height)];

     //You may want to set button titles here
     //or perform other customizations

    //Store the button in our array
    [self.myArray addObject:button];
    //Release the button (our array retains it for us)
    [button release];
  }  

  //
  // Show the buttons onscreen
  //

  for(UIButton *button in self.myButtons){
    //add the button to the view
    [self.view addSubview:button];
    //add the action
    [button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDown];
  }
}

- (void)someMethod:(id)sender{
   //Do something when the button was pressed;
}

- (void)dealloc{
  [self.myButtons release];
  [super dealloc];
}

@end

Now, you can go to button paradise without taking Interface Builder on the plane. Go UIButton happy!

like image 126
Moshe Avatar answered Nov 02 '22 05:11

Moshe


Like so:

NSMutableArray *arr = [NSMutableArray array];
for(int i = 1; i <= 39; ++i) {
  [arr addObject:[self valueForKey:[NSString stringWithFormat:@"btn%d", i]]];
}
like image 35
Jacob Relkin Avatar answered Nov 02 '22 03:11

Jacob Relkin


for(int i=1; i<totalButtonCount; i++) {
   NSString *buttonClassName = [NSString stringWithFormat:@"btn%d", i];
   [[NSClassFromString(buttonClassName) alloc] init];
   //add to array
}

Edit: after re-reading your question, this might not be what you're asking. I thought you wanted to create a bunch of instances of similar classes.

like image 4
Ben Scheirman Avatar answered Nov 02 '22 05:11

Ben Scheirman


You could create a getter method named after each button and than call [self valueForKey:]; for each but something here just screams "terrible design" :)

like image 3
Zaky German Avatar answered Nov 02 '22 04:11

Zaky German


Key Value programming is your friend

Basically you can loop around and create your buttons, and as long as you have properties defined for your buttons you can.

[self setObject:newButton forKey@"button1"];

where button1 is the same string as your variable name.

like image 2
Devraj Avatar answered Nov 02 '22 04:11

Devraj