0

I have 42 custom button on one View. How can I by pressing any of them edit of the created buttons that i want.

int a=0; int b=1; int otstup=10; for (int i=1; i<=42; i++) { CGRect frameBtn = CGRectMake(a+60+otstup, b+otstup, 45, 45); UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setFrame:frameBtn]; [button setBackgroundImage:[UIImage imageNamed:@"EmptyCoin.png"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside]; [button setTag:i]; [self.view addSubview:button]; a=a+50; if (i%7 == 0) { a=0; b=b+45; } } 
4
  • on clicking a button, Do you want to change the properties of that particular button? Please specify clearly what you want to do? Commented Mar 6, 2013 at 12:22
  • "by pressing any of them edit of the created buttons that i want." - huh? I don't understand. Commented Mar 6, 2013 at 12:23
  • I press the button 7. And the 8 button to change the picture. etc Commented Mar 6, 2013 at 12:31
  • But still the question is not very clear. What are you really asking for? Commented Mar 6, 2013 at 13:35

1 Answer 1

2
-(void)pressBtn:(id)sender{ UIButton *btn = (UIButton*)sender; if (btn.tag == 1){ 1st button tapped } else if(btn.tag == 2) { 2nd button tapped } } 

By using above code you can differentiate different buttons

Update

You have to create one mutable array store all the buttons in that array. you can access that array in pressBtn method

int a=0; int b=1; int otstup=10; for (int i=1; i<=42; i++) { CGRect frameBtn = CGRectMake(a+60+otstup, b+otstup, 45, 45); UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setFrame:frameBtn]; [button setBackgroundImage:[UIImage imageNamed:@"EmptyCoin.png"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside]; [button setTag:i]; [buttonAry addObject:button]; [self.view addSubview:button]; a=a+50; if (i%7 == 0) { a=0; b=b+45; } } 

Button action method

-(void)pressBtn:(id)sender{ UIButton *btn = (UIButton*)sender; if (btn.tag == 7){ UIButton *editButton = [buttonAry objectAtIndex:btn.tag+1]; [editButton setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal]; } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.