我想動態創建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…
}
感謝jhaliya,它的工作.. [(UIButton的*)[self.view viewWithTag:[發送方標籤]了setBackgroundImage:[UIImage的imageNamed: @「star2.png」] forState:UIControlStateNormal]; – Udhaya 2011-06-16 05:53:33
@Udhaya:很高興它幫助你.. – Jhaliya 2011-06-16 06:04:04