0

I have about 10-12 buttons that I'm adding to my scrollview. How can I make these into an array of buttons so that I can simplify the code? As of right now my code(only first three buttons are shown) is as follows:

    UIButton *redButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    redButton.frame = CGRectMake(0, 0, 50, 30);
    redButton.tag = 2;
    [redButton setTitle:@"red" forState:UIControlStateNormal];
    redButton.backgroundColor = [UIColor redColor];
    [redButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [redButton addTarget:self action:@selector(buttonAction:)    forControlEvents:UIControlEventTouchUpInside];
    [self.scollView addSubview:redButton];

    UIButton *blueButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    blueButton.frame = CGRectMake(70, 0, 50, 30);
    blueButton.tag = 3;
    [blueButton setTitle:@"blue" forState:UIControlStateNormal];
    blueButton.backgroundColor = [UIColor blueColor];
    [blueButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [blueButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.scollView addSubview:blueButton];

    UIButton *greenButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    greenButton.frame = CGRectMake(140, 0, 50, 30);
    greenButton.tag = 5 ;
    [greenButton setTitle:@"green" forState:UIControlStateNormal];
    greenButton.backgroundColor = [UIColor greenColor];
    [greenButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [greenButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.scollView addSubview:greenButton];

...

2
  • You mean to store the buttons (so you're simplifying using them), or to configure them (then really you mean how do I write a loop to configure my buttons)? Commented May 3, 2013 at 16:03
  • Sorry, yes, I meant a loop to configure. Commented May 3, 2013 at 16:14

3 Answers 3

2

Can you see if this is possible

- (void)addButtonsToScrollView
{
    NSArray *buttons = @[@{@"Tag":@2,@"Title":@"red",@"Color":[UIColor redColor]},
                         @{@"Tag":@3,@"Title":@"blue",@"Color":[UIColor blueColor]},
                         @{@"Tag":@5,@"Title":@"green",@"Color":[UIColor greenColor]}];

    CGRect frame = CGRectMake(0.0f, 0.0f, 50.0f, 30.0f);
    for (NSDictionary *dict in buttons)
    {
        UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = frame;
        button.tag = [dict[@"Tag"] integerValue];
        [button setTitle:dict[@"Title"]
                forState:UIControlStateNormal];
        button.backgroundColor = dict[@"Color"];
        [button setTitleColor:[UIColor blackColor]
                     forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:)
         forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
        frame.origin.x+=frame.size.width+20.0f;
    }

    CGSize contentSize = self.scrollView.frame.size;
    contentSize.width = frame.origin.x;
    self.scrollView.contentSize = contentSize;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use:

[NSArray arrayWithObjects:redButton,greenButton,blueButton,nil];

But might be better off using a NSDictionary

[NSDictionary dictionaryWithObjectsAndKeys:
                redButton, @"red",
                blueButton, @"blue",
                greenButton, @"green",
                nil];

That way you can use the key to look them up instead of index.

Comments

0

Ok, we have the solution, try this code mate,

enter image description here

-(void) createButtons{

    NSDictionary *buttonColors = @{@"Red":[UIColor redColor],@"Green":[UIColor greenColor],@"Black":[UIColor blackColor],@"Yellow":[UIColor yellowColor],@"Blue":[UIColor blueColor]};

    int tag = 1;
    for(NSString *key in buttonColors.allKeys){
        UIColor *color = [buttonColors objectForKey:key];
        UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
        CGRect frame = CGRectMake(((tag -1)*70), 0, 50, 30);
        [button setFrame:frame];
        button.tag = tag;
        button.backgroundColor = color;
        [button setTitleColor:color forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

        [button setTitle:key forState:UIControlStateNormal];
        [self.scrollView addSubview:button];

        tag++;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.