achiral
achiral

Reputation: 601

Objective-C: An array of UIButtons

If I have a large number of buttons set up in Interface Builder, how can I place them into an array via code?

For example, if I have three buttons and in the interface file I define them:

@property (nonatomic, retain) IBOutlet UIButton *button1;
@property (nonatomic, retain) IBOutlet UIButton *button2;
@property (nonatomic, retain) IBOutlet UIButton *button3;

If I want to rename each of these buttons to say "Hello", I want to be able to lump them into an array and then use a function such as:

for (int i = 0; i < someArray.count; i++)
{
    someArray[i].text = @"Hello";
}

Could someone please provide information on how this is achieved (if it's possible)?

Thanks

Upvotes: 7

Views: 12820

Answers (5)

ennuikiller
ennuikiller

Reputation: 46965

Assuming the buttons in IB are correctly wired to the IBOutlet properties then its as straightforward as:

NSArray *buttonArray = [[NSArray alloc] initWithObjects:button1,button2,button3,nil];

Upvotes: 6

carlos
carlos

Reputation: 41

I think that the easiest is to create a single button that you will init and add to a mutable array:

in *.m:

UIButton *buttonChar;

in your function:

buttonArray = [[NSMutableArray alloc] init];

for (int i=0; i<lenghtWord; i++) {
    buttonChar = [[UIButton alloc] initWithFrame:CGRectMake(.2*size.height+(float)i*charFontSize, .5*size.width, charFontSize, charFontSize)];
    buttonChar.tag = i;

    [buttonChar setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [buttonChar setTitle:[[NSString alloc] formatWithString:@"Button %d",i] forState:UIControlStateNormal];
    buttonChar.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    buttonChar.titleLabel.textAlignment = UITextAlignmentCenter;
    buttonChar.layer.cornerRadius = 5;
    buttonChar.layer.masksToBounds = YES;
    buttonChar.layer.borderWidth = 1;
    buttonChar.layer.borderColor = [[UIColor blackColor] CGColor];
    [buttonChar setBackgroundColor:[UIColor whiteColor]];
    [buttonChar addTarget:self action:@selector(buttonAction:withEvent:) forControlEvents:UIControlEventTouchDragInside];
    [buttonArray addObject:buttonChar];
}

buttons are available through:

[[buttonArray objectAtIndex:j] setTitle:@" " forState:UIControlStateNormal];

Since the tag option is also filled, I can always tell what button from the array was touched if it was displayed (buttonAction:withEvent:)

Cheers

Upvotes: 0

WrightsCS
WrightsCS

Reputation: 50697

The following code will add 5 buttons, each with a new tag and also connect it to an event. The buttons will be placed side by side with a 5px padding:

If you plan on accessing the button outside of the for{} scope below, you can define your button in your header, otherwise, you can define it inside the scope. UIButton * settButton;

.m

CGFloat staticX         = 5;    // Static X for all buttons.
CGFloat staticWidth     = 60;   // Static Width for all Buttons. 
CGFloat staticHeight    = 56;   // Static Height for all buttons.
CGFloat staticPadding   = 5;    // Padding to add between each button.

for (int i = 0; i < 5; i++) 
{
    settButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [settButton setTag:i];
    [settButton setFrame:CGRectMake((staticX + (i * (staticHeight + staticPadding))),5,staticWidth,staticHeight)];
    [settButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%i.png",i]] forState:UIControlStateNormal];
    [settButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchDown];
    [settButtonView addSubview: settButton];
}
[self.view addSubview: settButtonView];

Upvotes: 8

Lachlan Roche
Lachlan Roche

Reputation: 25946

Another approach is to wire the buttons up to an NSArray in Interface Builder.

@property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *buttons;

Upvotes: 14

Matthew Gillingham
Matthew Gillingham

Reputation: 3429

Since you created the buttons directly in Interface Builder, can't you just set them directly in code?

A more general solution would be something like this...if I assume two things: that they are all subviews of the UIViewController's view and that these UIButtons are the only UIButtons in that view, you can do something like this:

for (UIView *aView in [self.view subviews]) {
   if ([aView isKindOfClass:[UIButton class]) {
      UIButton *aButton = (UIButton *) aButton;
      [aButton setTitle:@"Hello" forState:UIControlStateNormal];
   }
}

Upvotes: 2

Related Questions