2013-03-06 153 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