0

我正在创建一个包含动态页面的应用程序。实际上,我通过检查来自服务器的响应创建了一些动态问题和 UIButtons(自定义复选框)。我的代码在这里,

  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时,它无法处理按钮操作。我需要像复选框控件一样更改它,我将如何从一组按钮访问动态创建的按钮的控件?

我想你可以理解我的要求,提前谢谢..

4

2 回答 2

0

在您的代码中,您需要像这样更改

试试这个代码

 if ([comSt compare: @"CheckBoxList"]==NSOrderedSame)
    {
        int count=[ResponseFromServer count];

        for(int i=0;i<count;i++)
        {
            button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            button.tag=0; 
            [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];   

             [button setTag:100];

             }

             else
             {

                 [button setSelected:YES];
                 [button setImage:[UIImage imageNamed:@"uncheck.png" forState:UIControlStateSelected];   

                  [button setTag:0];
                  }
 } 
于 2012-07-02T05:55:10.910 回答
0

同时添加动态按钮集[button setSelected:NO];

替换方法并添加下面给出的一种方法

  -(void)checkButtonTapped:(id)sender
{

  UIButton *curButton = (UIButton*)sender 

  if (curButton.tag==0) 
  {
     [self buttonAction:curButton];
  }

  if (curButton.tag==1) 
  {
      [self buttonAction:curButton];
  }

  // continues
 }


 -(void)buttonAction:(UIButton *)curButton
 {

   if(![curButton isSelected])
     {
       [curButton setSelected:YES];
       [curButton setImage:[UIImage imageNamed:@"check.png" forState:UIControlStateSelected];  //not working, button image is not changing 
     } else {
       [curButton setSelected:NO];
       [curButton setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];

     }
 }
于 2012-07-02T05:56:55.577 回答