0

I would like to add UIButton using for loop. Each button would have a different action. My code is:

NSArray *methods = [[NSArray alloc]initWithObjects:@"1",@"2", @"3", @"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15", nil];
int x=0;
for (int t=0;t<=14;t++) {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector([methods[t]]) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"" forState:UIControlStateNormal];
    button.frame = CGRectMake(275, x+8, 28, 16);
    [button setBackgroundColor:[UIColor colorWithRed: 10.0/255.0f green:100.0/255.0f blue:150.0/255.0f alpha:1.0f]];
    button.layer.cornerRadius = 10;
    [button setBackgroundImage:[UIImage imageNamed:@"Plus"] forState:UIControlStateNormal];
    [segmentedView1 addSubview:button];
    [self.view addSubview:segmentedView1];
    x+=20;
}

Stuck with an error: "Expected identifier" in the line [button addTarget:self action:@selector([methods[t]])

Also,how do i make outlets for such buttons added programatically?

Any solution? Thank you

3

3 Answers 3

2

I would advise to assign same selector for all the buttons. Pointer to the button will be send to this selector and you can recognize which button was pressed using .tag property for example.

for (int t=0;t<=14;t++) {
   ...
   [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
   button.tag = i;
}

- (void)buttonPressed:(id)sender {
   if (((UIButton *)sender).tag == 1) {
      ... 
   }
   ...
}

Also move [self.view addSubview:segmentedView1]; outside of your loop.

Sign up to request clarification or add additional context in comments.

8 Comments

can u please elaborate using this .tag property?
Got it..thanks..But a small correction here.."buttonPressed:" ..double quotes are not required.Cheers
Or even without any quotes.
Can u please tell me how do i make oultets for these kind of buttons added programatically?
Why do you need outlets for them? Outlets are to bind elements from the storyboard to properties.
|
1
NSString *selectorName = methods[t];
SEL method = NSSelectorFromString(selectorName);

[button addTarget:self action:method forControlEvents:UIControlEventTouchUpInside];

don't forget, you have to implement all these selectors and if you keep methods in array it's potentially pretty risky. I would suggest to use one action for all buttons and handle it via tag property inside your action.

If not, try to wrap:

if ([self respondsToSelector:method]) {
    [button addTarget:self action:method forControlEvents:UIControlEventTouchUpInside];
}

5 Comments

This is not the right way to implement many control behaving in similar way. You should combine all handlers into one.
you should have added this comment to original question, I've just answered on it, how to avoid such error
This is actually quite wrong I think. If you see that OP is going wrong direction with his approach - you should inform him about it, and not answering exact question and letting him go even further.
agree >> if you keep methods in array it's potentially pretty risky.
Can u please tell me how do i make oultets for these kind of buttons added programatically?
0

@selector expects a method, not an object

you have to do like

- (void)onClick:(UIBUtton*)sender {
    if(sender.tag == 1) {
        // ....
    }
}

button.tag = i;
[button addTarget:self action:@selector(onClick)

4 Comments

Yes..but each button would have a different action in my case..How to make this possible>
You have to switch your button inside the handler. I updated my answer.
Can u please tell me how do i make oultets for these kind of buttons added programatically?
There is no need for outlets since they are to connect interfacebuilder to code. Since your buttons are genereated in code, you already have full access to them.

Your Answer

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