Note that there are some explanatory texts on larger screens.

plurals
  1. POIBAction used with Dynamically added UIbuttons
    text
    copied!<p>Good Evening all!</p> <p>I have some UIButtons added dynamically into my view and of course I have an IBAction which handles button events. </p> <p>The problem is: How can I detect which button is pressed if the only thing I know is the (id)sender and the array of buttons? </p> <p>Buttons are never the same, every button has a different behavior. When I want to use static buttons and connect them through the IB I use something like this :</p> <pre><code>-(IBAction)doSomething:(id)sender { if(sender == button1) dosomething; if(sender == button2) dosomething else; if(sender == button3) dosomething3; } </code></pre> <p>In my case this does not work because there is no button1, button2, button3 but a MutableArray of buttons which have the same name as they were allocated with it. Button!</p> <p>I tried using the way above but with no success and i tried also getting the tag of a button but I have nothing to compare it to!</p> <p>I would really appreciate your help.</p> <p>sincerely<br> L_Sonic PS Dynamicaly means that i am creating the buttons in random time during run time like this</p> <pre><code>-(void)configActionSheetView { buttonView = [[UIView alloc]initWithFrame:CGRectMake(0.0,460, 60, 480)]; [buttonView setBackgroundColor:[UIColor blackColor]]; [buttonView setAlpha:0.6]; for (int i = 0 ;i&lt;[buffButtons count];i++) { UIButton *customButton = [buffButtons objectAtIndex:i]; customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //UILabel *customLabel = [[UILabel alloc]init]; //[customButton setTag:(i)+11]; [customButton addTarget:self action:@selector(activateBuffEffect:) forControlEvents:UIControlEventTouchUpInside]; [customButton setAlpha:1.0]; customButton.frame = CGRectMake(8.0, 5+(50*i), 44.0, 44.0); [customButton setTitle:nil forState:UIControlStateNormal]; buttonView.frame = CGRectMake(0, 460, 60, 50+(44*(i+1))); [buttonView addSubview:customButton]; } } </code></pre> <p>this is inside a functions and gets called during run time. the buffButtons is a mutableArray with buttons that gets populated during runtime. i need a solution like this i cannot get a different eventhandling method for everybutton. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload