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!