iPhone – Add multiple buttons to the view to the view, call the same method, determine which button it is

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

I can make one or more like this UIButton (in a loop, but shortened for simplicity):

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self < br /> 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];

Copy/Edit this link:
How do I create a basic UIButton programmatically?

But how to determine in buttonClicked: which button was clicked? If possible, I want to pass label data to identify the button.

The position (such as an array) keeps a reference to the actual button object, or sets the button’s label to something useful (such as an offset in some other data array). For example (use a label, because this usually has to be useful): < p>

for( int i = 0; i <5; i++) {
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[ aButton setTag:i];
[aButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[aView addSubview:aButton];
}

// then ...

- (void)buttonClicked:(UIButton*)button
{
NSLog(@"Button %ld clicked.", (long int )[button tag]);
}

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

I can make one or more UIButtons like this (in a loop, but shortened for simplicity):

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

Copy/Edit this link:
How do I create a basic UIButton programmatically?

But how to determine in buttonClicked: which button was clicked? If possible, I want to pass label data to identify the button.

You can keep a reference to the actual button object in an important location (such as an array) , Or set the button’s tag to something useful (such as an offset in some other data array). For example (use a label, because this usually must be useful):

for( int i = 0; i <5; i++) {
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTag:i];
[aButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[aView addSubview:aButton];
}

// then ...

- (void)buttonClicked:(UIButton*)button
{
NSLog(@"Button %ld clicked.", (long int)[button tag]);
}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1059 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.