2011-06-15 155 views
0

我想動態創建uibuttons。所以我爲循環創建帶有標籤的按鈕並添加到視圖中。 所有按鈕都會對其標籤值執行相同的操作...但我想更改該按鈕的屬性。因此,我需要獲得使用標籤值的UIButton ...如何創建動態按鈕iphone xcode?

我的代碼...

UIButton *button2; 
//view did load 
int width = 30; 

for(int i = 1; i <= 5; i++) 
{ 
    button2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button2 addTarget:self action:@selector(ratingAction:) forControlEvents:UIControlEventTouchUpInside]; 
    [button2 setBackgroundImage:[UIImage imageNamed:@"star1.png"] forState:UIControlStateNormal]; 
    button2.tag = i; 
    button2.backgroundColor = [UIColor clearColor]; 
    button2.frame = CGRectMake(width, 78, 15, 15); 

    [self.view addSubview:button2]; 

    width = width +30; 
} 

-(void)ratingAction:(id*)sender 
{ 
    // here using the tag value i want to get the button and change the background image…. 
    // for example i want to change the background for tag values 1,3,6,7 ,8… 
} 

回答

1

使用viewWithTagUIView功能使用標記值來訪問你的UIButton

查看文檔viewWithTag

如下使用它。

UIButton* myButton = (UIButton*)[mySuperView viewWithTag:1]; 
UIButton* myButton = (UIButton*)[mySuperView viewWithTag:3]; 
UIButton* myButton = (UIButton*)[mySuperView viewWithTag:6]; 
......... 
+0

感謝jhaliya,它的工作.. [(UIButton的*)[self.view viewWithTag:[發送方標籤]了setBackgroundImage:[UIImage的imageNamed: @「star2.png」] forState:UIControlStateNormal]; – Udhaya 2011-06-16 05:53:33

+0

@Udhaya:很高興它幫助你.. – Jhaliya 2011-06-16 06:04:04

1
-(void)ratingAction:(id*)sender 
{ 
    // here using the tag value i want to get the button and change the background image…. 
    // for example i want to change the background for tag values 1,3,6,7 ,8…  
    if ([sender isKindOfClass:[UIButton class]]) 
    { 
     UIButton *temp=(UIButton*)sender; 

     if ([temp tag]==1 || [temp tag]==3 || [temp tag]==6 || [temp tag]==7) 
     { 
      [temp setBackgroundColor:[UIColor redColor]]; 
     } 
    } 
} 
+0

其實temp是一個本地按鈕,如果我們setBackgroundColor到臨時它不會影響superView中的按鈕..我是嗎? – Udhaya 2011-06-16 05:34:57

0

檢查這個代碼

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    int y = 100; 
    for (int i=0; i<=5; i++) { 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [button addTarget:self 
        action:@selector(aMethod:) 
     forControlEvents:UIControlEventTouchDown]; 
     [button setTitle:@"Show View" forState:UIControlStateNormal]; 
     button.frame = CGRectMake(80, y, 160, 40); 
     [button setTitle:[NSString stringWithFormat:@"Button-%i", i] forState:UIControlStateNormal]; 
//  [button set 

     [self.view addSubview:button]; 
     y=y+60; 
    } 
} 

-(void)aMethod:(id)sender{ 
    UIButton *button = sender; 
    int y = 100; 
    int i = 5; 

    for (int x=0; x<=i; x++) { 
     NSString *frameString = [NSString stringWithFormat:@"{{80, %i}, {160, 40}}", y]; 
     if ([NSStringFromCGRect(button.frame) isEqualToString:frameString]) { 
      NSLog(@"button %i clicked..", x); 
     } 
    y= y+60; 
    } 
} 
+0

這種方法效果很好。 – Sri 2012-09-19 14:15:16