我正在創建一個包含動態頁面的應用程序。實際上,我通過檢查服務器的響應,創建了一些動態問題和UIButtons(自定義複選框)。 我的代碼是在這裏,在iphone中的動態UIButton控制和操作?
NSString *response=[ResponseFromServer ObjectAtIndex:0];
if ([comSt compare: @"CheckBoxList"]==NSOrderedSame)
{
int count=[ResponseFromServer count];
for(int i=0;i<count;i++)
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=i;
[button setFrame:CGRectMake(0.0f, 0.0f, 37, 37)];
[button setCenter:CGPointMake(116.0,p1)];
[button setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor whiteColor]];
[button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:button];
p1=p1+30;
}
-(void)checkButtonTapped:(id)sender
{
int tagVal = [(UIButton*)sender tag];
if (tagVal==0) {
NSLog(@"tag = 0");
[button setSelected:YES];
[button setImage:[UIImage imageNamed:@"check.png" forState:UIControlStateSelected]; //not working, button image is not changing
}
假設我已經創建了5個動態按鈕與不同的標籤。我首先將所有這些按鈕的圖像設置爲uncheck.png。有用。 但是當我點擊一個特定的按鈕,並需要將該按鈕圖像更改爲checked.png,它不在按鈕操作上工作。 我需要改變它像複選框控件,我將如何訪問從一組按鈕動態創建的按鈕的控制?
我覺得你能理解我的要求,在此先感謝..
給出一個方法只需要爲一個按鈕 –
是的,我只是檢查只按鈕設置條件。 – neerajPK