Add a multiple buttons to a view programatically, call the same method, determine which button it wa

Posted by just_another_coder on Stack Overflow See other posts from Stack Overflow or by just_another_coder
Published on 2010-04-15T14:44:46Z Indexed on 2010/04/15 14:53 UTC
Read the original article Hit count: 100

Filed under:
|
|

I want to programatically add multiple UIButtons to a view - the number of buttons is unknown at compile time.

I can make one or more UIButton's like so (in a loop, but shorted for simplicity):

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
       action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Button x" forState:UIControlStateNormal];
button.frame = CGRectMake(100.0, 100.0, 120.0, 50.0);
[view addSubview:button];

Copied/Edited from this link: http://stackoverflow.com/questions/1378765/how-do-i-create-a-basic-uibutton-programmatically

But how do I determine in buttonClicked: which button was clicked? I'd like to pass tag data if possible to identify the button.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about xcode