2013-03-06 156 views
0

我在一个视图上有42个自定义按钮。我怎样才能通过按他们中的任何一个编辑我想要创建的按钮。自定义按钮动作

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; 
    } 
} 
+0

上点击一个按钮,你想改变这种特定的按钮的属性?请明确指出你想要做什么? – 2013-03-06 12:22:30

+0

“,通过按下其中任何一个编辑我想要创建的按钮。” - 嗯?我不明白。 – 2013-03-06 12:23:12

+0

我按下按钮7.和8按钮更改图片。等 – user2128989 2013-03-06 12:31:59

回答

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

} 
else if(btn.tag == 2) 
{ 
2nd button tapped 
} 
} 

通过使用上面的代码就可以区分不同的按钮

更新

您有数组中创建一个可变数组存储中的所有按钮。您可以访问阵列pressBtn方法

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; 
} 
} 

按钮的操作方法

-(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]; 
} 

} 
+0

我根据您的要求更新了答案 – thavasidurai 2013-03-06 13:05:15

+0

我再次更新 – thavasidurai 2013-03-06 13:14:08